1
2#ifndef _LINUX_SCATTERLIST_H
3#define _LINUX_SCATTERLIST_H
4
5#include <linux/string.h>
6#include <linux/types.h>
7#include <linux/bug.h>
8#include <linux/mm.h>
9#include <asm/io.h>
10
11struct scatterlist {
12 unsigned long page_link;
13 unsigned int offset;
14 unsigned int length;
15 dma_addr_t dma_address;
16#ifdef CONFIG_NEED_SG_DMA_LENGTH
17 unsigned int dma_length;
18#endif
19};
20
21
22
23
24
25#define SCATTERLIST_MAX_SEGMENT (UINT_MAX & PAGE_MASK)
26
27
28
29
30
31
32
33
34#define sg_dma_address(sg) ((sg)->dma_address)
35
36#ifdef CONFIG_NEED_SG_DMA_LENGTH
37#define sg_dma_len(sg) ((sg)->dma_length)
38#else
39#define sg_dma_len(sg) ((sg)->length)
40#endif
41
42struct sg_table {
43 struct scatterlist *sgl;
44 unsigned int nents;
45 unsigned int orig_nents;
46};
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64#define SG_CHAIN 0x01UL
65#define SG_END 0x02UL
66
67
68
69
70
71
72#define sg_is_chain(sg) ((sg)->page_link & SG_CHAIN)
73#define sg_is_last(sg) ((sg)->page_link & SG_END)
74#define sg_chain_ptr(sg) \
75 ((struct scatterlist *) ((sg)->page_link & ~(SG_CHAIN | SG_END)))
76
77
78
79
80
81
82
83
84
85
86
87static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
88{
89 unsigned long page_link = sg->page_link & (SG_CHAIN | SG_END);
90
91
92
93
94
95 BUG_ON((unsigned long) page & (SG_CHAIN | SG_END));
96#ifdef CONFIG_DEBUG_SG
97 BUG_ON(sg_is_chain(sg));
98#endif
99 sg->page_link = page_link | (unsigned long) page;
100}
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116static inline void sg_set_page(struct scatterlist *sg, struct page *page,
117 unsigned int len, unsigned int offset)
118{
119 sg_assign_page(sg, page);
120 sg->offset = offset;
121 sg->length = len;
122}
123
124static inline struct page *sg_page(struct scatterlist *sg)
125{
126#ifdef CONFIG_DEBUG_SG
127 BUG_ON(sg_is_chain(sg));
128#endif
129 return (struct page *)((sg)->page_link & ~(SG_CHAIN | SG_END));
130}
131
132
133
134
135
136
137
138
139static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
140 unsigned int buflen)
141{
142#ifdef CONFIG_DEBUG_SG
143 BUG_ON(!virt_addr_valid(buf));
144#endif
145 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
146}
147
148
149
150
151#define for_each_sg(sglist, sg, nr, __i) \
152 for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
153
154
155
156
157
158
159
160
161
162
163
164static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents,
165 struct scatterlist *sgl)
166{
167
168
169
170 prv[prv_nents - 1].offset = 0;
171 prv[prv_nents - 1].length = 0;
172
173
174
175
176
177 prv[prv_nents - 1].page_link = ((unsigned long) sgl | SG_CHAIN)
178 & ~SG_END;
179}
180
181
182
183
184
185
186
187
188
189
190static inline void sg_mark_end(struct scatterlist *sg)
191{
192
193
194
195 sg->page_link |= SG_END;
196 sg->page_link &= ~SG_CHAIN;
197}
198
199
200
201
202
203
204
205
206
207static inline void sg_unmark_end(struct scatterlist *sg)
208{
209 sg->page_link &= ~SG_END;
210}
211
212
213
214
215
216
217
218
219
220
221
222static inline dma_addr_t sg_phys(struct scatterlist *sg)
223{
224 return page_to_phys(sg_page(sg)) + sg->offset;
225}
226
227
228
229
230
231
232
233
234
235
236
237static inline void *sg_virt(struct scatterlist *sg)
238{
239 return page_address(sg_page(sg)) + sg->offset;
240}
241
242
243
244
245
246
247
248static inline void sg_init_marker(struct scatterlist *sgl,
249 unsigned int nents)
250{
251 sg_mark_end(&sgl[nents - 1]);
252}
253
254int sg_nents(struct scatterlist *sg);
255int sg_nents_for_len(struct scatterlist *sg, u64 len);
256struct scatterlist *sg_next(struct scatterlist *);
257struct scatterlist *sg_last(struct scatterlist *s, unsigned int);
258void sg_init_table(struct scatterlist *, unsigned int);
259void sg_init_one(struct scatterlist *, const void *, unsigned int);
260int sg_split(struct scatterlist *in, const int in_mapped_nents,
261 const off_t skip, const int nb_splits,
262 const size_t *split_sizes,
263 struct scatterlist **out, int *out_mapped_nents,
264 gfp_t gfp_mask);
265
266typedef struct scatterlist *(sg_alloc_fn)(unsigned int, gfp_t);
267typedef void (sg_free_fn)(struct scatterlist *, unsigned int);
268
269void __sg_free_table(struct sg_table *, unsigned int, bool, sg_free_fn *);
270void sg_free_table(struct sg_table *);
271int __sg_alloc_table(struct sg_table *, unsigned int, unsigned int,
272 struct scatterlist *, gfp_t, sg_alloc_fn *);
273int sg_alloc_table(struct sg_table *, unsigned int, gfp_t);
274int __sg_alloc_table_from_pages(struct sg_table *sgt, struct page **pages,
275 unsigned int n_pages, unsigned int offset,
276 unsigned long size, unsigned int max_segment,
277 gfp_t gfp_mask);
278int sg_alloc_table_from_pages(struct sg_table *sgt, struct page **pages,
279 unsigned int n_pages, unsigned int offset,
280 unsigned long size, gfp_t gfp_mask);
281
282#ifdef CONFIG_SGL_ALLOC
283struct scatterlist *sgl_alloc_order(unsigned long long length,
284 unsigned int order, bool chainable,
285 gfp_t gfp, unsigned int *nent_p);
286struct scatterlist *sgl_alloc(unsigned long long length, gfp_t gfp,
287 unsigned int *nent_p);
288void sgl_free_n_order(struct scatterlist *sgl, int nents, int order);
289void sgl_free_order(struct scatterlist *sgl, int order);
290void sgl_free(struct scatterlist *sgl);
291#endif
292
293size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf,
294 size_t buflen, off_t skip, bool to_buffer);
295
296size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
297 const void *buf, size_t buflen);
298size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
299 void *buf, size_t buflen);
300
301size_t sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents,
302 const void *buf, size_t buflen, off_t skip);
303size_t sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents,
304 void *buf, size_t buflen, off_t skip);
305size_t sg_zero_buffer(struct scatterlist *sgl, unsigned int nents,
306 size_t buflen, off_t skip);
307
308
309
310
311
312#define SG_MAX_SINGLE_ALLOC (PAGE_SIZE / sizeof(struct scatterlist))
313
314
315
316
317
318
319
320
321#define SG_CHUNK_SIZE 128
322
323
324
325
326
327#ifdef CONFIG_ARCH_HAS_SG_CHAIN
328#define SG_MAX_SEGMENTS 2048
329#else
330#define SG_MAX_SEGMENTS SG_CHUNK_SIZE
331#endif
332
333#ifdef CONFIG_SG_POOL
334void sg_free_table_chained(struct sg_table *table, bool first_chunk);
335int sg_alloc_table_chained(struct sg_table *table, int nents,
336 struct scatterlist *first_chunk);
337#endif
338
339
340
341
342
343
344
345
346
347
348
349struct sg_page_iter {
350 struct scatterlist *sg;
351 unsigned int sg_pgoffset;
352
353
354 unsigned int __nents;
355 int __pg_advance;
356
357};
358
359bool __sg_page_iter_next(struct sg_page_iter *piter);
360void __sg_page_iter_start(struct sg_page_iter *piter,
361 struct scatterlist *sglist, unsigned int nents,
362 unsigned long pgoffset);
363
364
365
366
367static inline struct page *sg_page_iter_page(struct sg_page_iter *piter)
368{
369 return nth_page(sg_page(piter->sg), piter->sg_pgoffset);
370}
371
372
373
374
375
376
377static inline dma_addr_t sg_page_iter_dma_address(struct sg_page_iter *piter)
378{
379 return sg_dma_address(piter->sg) + (piter->sg_pgoffset << PAGE_SHIFT);
380}
381
382
383
384
385
386
387
388
389#define for_each_sg_page(sglist, piter, nents, pgoffset) \
390 for (__sg_page_iter_start((piter), (sglist), (nents), (pgoffset)); \
391 __sg_page_iter_next(piter);)
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409#define SG_MITER_ATOMIC (1 << 0)
410#define SG_MITER_TO_SG (1 << 1)
411#define SG_MITER_FROM_SG (1 << 2)
412
413struct sg_mapping_iter {
414
415 struct page *page;
416 void *addr;
417 size_t length;
418 size_t consumed;
419 struct sg_page_iter piter;
420
421
422 unsigned int __offset;
423 unsigned int __remaining;
424 unsigned int __flags;
425};
426
427void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
428 unsigned int nents, unsigned int flags);
429bool sg_miter_skip(struct sg_mapping_iter *miter, off_t offset);
430bool sg_miter_next(struct sg_mapping_iter *miter);
431void sg_miter_stop(struct sg_mapping_iter *miter);
432
433#endif
434