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, unsigned int,
270 sg_free_fn *);
271void sg_free_table(struct sg_table *);
272int __sg_alloc_table(struct sg_table *, unsigned int, unsigned int,
273 struct scatterlist *, unsigned int, gfp_t, sg_alloc_fn *);
274int sg_alloc_table(struct sg_table *, unsigned int, gfp_t);
275int __sg_alloc_table_from_pages(struct sg_table *sgt, struct page **pages,
276 unsigned int n_pages, unsigned int offset,
277 unsigned long size, unsigned int max_segment,
278 gfp_t gfp_mask);
279int sg_alloc_table_from_pages(struct sg_table *sgt, struct page **pages,
280 unsigned int n_pages, unsigned int offset,
281 unsigned long size, gfp_t gfp_mask);
282
283#ifdef CONFIG_SGL_ALLOC
284struct scatterlist *sgl_alloc_order(unsigned long long length,
285 unsigned int order, bool chainable,
286 gfp_t gfp, unsigned int *nent_p);
287struct scatterlist *sgl_alloc(unsigned long long length, gfp_t gfp,
288 unsigned int *nent_p);
289void sgl_free_n_order(struct scatterlist *sgl, int nents, int order);
290void sgl_free_order(struct scatterlist *sgl, int order);
291void sgl_free(struct scatterlist *sgl);
292#endif
293
294size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf,
295 size_t buflen, off_t skip, bool to_buffer);
296
297size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
298 const void *buf, size_t buflen);
299size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
300 void *buf, size_t buflen);
301
302size_t sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents,
303 const void *buf, size_t buflen, off_t skip);
304size_t sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents,
305 void *buf, size_t buflen, off_t skip);
306size_t sg_zero_buffer(struct scatterlist *sgl, unsigned int nents,
307 size_t buflen, off_t skip);
308
309
310
311
312
313#define SG_MAX_SINGLE_ALLOC (PAGE_SIZE / sizeof(struct scatterlist))
314
315
316
317
318
319
320
321
322#define SG_CHUNK_SIZE 128
323
324
325
326
327
328#ifdef CONFIG_ARCH_NO_SG_CHAIN
329#define SG_MAX_SEGMENTS SG_CHUNK_SIZE
330#else
331#define SG_MAX_SEGMENTS 2048
332#endif
333
334#ifdef CONFIG_SG_POOL
335void sg_free_table_chained(struct sg_table *table,
336 unsigned nents_first_chunk);
337int sg_alloc_table_chained(struct sg_table *table, int nents,
338 struct scatterlist *first_chunk,
339 unsigned nents_first_chunk);
340#endif
341
342
343
344
345
346
347
348
349
350
351
352struct sg_page_iter {
353 struct scatterlist *sg;
354 unsigned int sg_pgoffset;
355
356
357 unsigned int __nents;
358 int __pg_advance;
359
360};
361
362
363
364
365
366
367
368
369struct sg_dma_page_iter {
370 struct sg_page_iter base;
371};
372
373bool __sg_page_iter_next(struct sg_page_iter *piter);
374bool __sg_page_iter_dma_next(struct sg_dma_page_iter *dma_iter);
375void __sg_page_iter_start(struct sg_page_iter *piter,
376 struct scatterlist *sglist, unsigned int nents,
377 unsigned long pgoffset);
378
379
380
381
382static inline struct page *sg_page_iter_page(struct sg_page_iter *piter)
383{
384 return nth_page(sg_page(piter->sg), piter->sg_pgoffset);
385}
386
387
388
389
390
391
392static inline dma_addr_t
393sg_page_iter_dma_address(struct sg_dma_page_iter *dma_iter)
394{
395 return sg_dma_address(dma_iter->base.sg) +
396 (dma_iter->base.sg_pgoffset << PAGE_SHIFT);
397}
398
399
400
401
402
403
404
405
406
407
408#define for_each_sg_page(sglist, piter, nents, pgoffset) \
409 for (__sg_page_iter_start((piter), (sglist), (nents), (pgoffset)); \
410 __sg_page_iter_next(piter);)
411
412
413
414
415
416
417
418
419
420
421
422#define for_each_sg_dma_page(sglist, dma_iter, dma_nents, pgoffset) \
423 for (__sg_page_iter_start(&(dma_iter)->base, sglist, dma_nents, \
424 pgoffset); \
425 __sg_page_iter_dma_next(dma_iter);)
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443#define SG_MITER_ATOMIC (1 << 0)
444#define SG_MITER_TO_SG (1 << 1)
445#define SG_MITER_FROM_SG (1 << 2)
446
447struct sg_mapping_iter {
448
449 struct page *page;
450 void *addr;
451 size_t length;
452 size_t consumed;
453 struct sg_page_iter piter;
454
455
456 unsigned int __offset;
457 unsigned int __remaining;
458 unsigned int __flags;
459};
460
461void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
462 unsigned int nents, unsigned int flags);
463bool sg_miter_skip(struct sg_mapping_iter *miter, off_t offset);
464bool sg_miter_next(struct sg_mapping_iter *miter);
465void sg_miter_stop(struct sg_mapping_iter *miter);
466
467#endif
468