xref: /linux/include/linux/scatterlist.h (revision 27b9989b87119da2f33f2c0fcbb8984ab4ebdf1a)
1 /* SPDX-License-Identifier: GPL-2.0 */
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 
11 struct 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 #ifdef CONFIG_NEED_SG_DMA_FLAGS
20 	unsigned int    dma_flags;
21 #endif
22 };
23 
24 /*
25  * These macros should be used after a dma_map_sg call has been done
26  * to get bus addresses of each of the SG entries and their lengths.
27  * You should only work with the number of sg entries dma_map_sg
28  * returns, or alternatively stop on the first sg_dma_len(sg) which
29  * is 0.
30  */
31 #define sg_dma_address(sg)	((sg)->dma_address)
32 
33 #ifdef CONFIG_NEED_SG_DMA_LENGTH
34 #define sg_dma_len(sg)		((sg)->dma_length)
35 #else
36 #define sg_dma_len(sg)		((sg)->length)
37 #endif
38 
39 struct sg_table {
40 	struct scatterlist *sgl;	/* the list */
41 	unsigned int nents;		/* number of mapped entries */
42 	unsigned int orig_nents;	/* original size of list */
43 };
44 
45 struct sg_append_table {
46 	struct sg_table sgt;		/* The scatter list table */
47 	struct scatterlist *prv;	/* last populated sge in the table */
48 	unsigned int total_nents;	/* Total entries in the table */
49 };
50 
51 /*
52  * Notes on SG table design.
53  *
54  * We use the unsigned long page_link field in the scatterlist struct to place
55  * the page pointer AND encode information about the sg table as well. The two
56  * lower bits are reserved for this information.
57  *
58  * If bit 0 is set, then the page_link contains a pointer to the next sg
59  * table list. Otherwise the next entry is at sg + 1.
60  *
61  * If bit 1 is set, then this sg entry is the last element in a list.
62  *
63  * See sg_next().
64  *
65  */
66 
67 #define SG_CHAIN	0x01UL
68 #define SG_END		0x02UL
69 
70 /*
71  * We overload the LSB of the page pointer to indicate whether it's
72  * a valid sg entry, or whether it points to the start of a new scatterlist.
73  * Those low bits are there for everyone! (thanks mason :-)
74  */
75 #define SG_PAGE_LINK_MASK (SG_CHAIN | SG_END)
76 
__sg_flags(struct scatterlist * sg)77 static inline unsigned int __sg_flags(struct scatterlist *sg)
78 {
79 	return sg->page_link & SG_PAGE_LINK_MASK;
80 }
81 
sg_chain_ptr(struct scatterlist * sg)82 static inline struct scatterlist *sg_chain_ptr(struct scatterlist *sg)
83 {
84 	return (struct scatterlist *)(sg->page_link & ~SG_PAGE_LINK_MASK);
85 }
86 
sg_is_chain(struct scatterlist * sg)87 static inline bool sg_is_chain(struct scatterlist *sg)
88 {
89 	return __sg_flags(sg) & SG_CHAIN;
90 }
91 
sg_is_last(struct scatterlist * sg)92 static inline bool sg_is_last(struct scatterlist *sg)
93 {
94 	return __sg_flags(sg) & SG_END;
95 }
96 
97 /**
98  * sg_next - return the next scatterlist entry in a list
99  * @sg:		The current sg entry
100  *
101  * Description:
102  *   Usually the next entry will be @sg + 1, but if this sg element is part
103  *   of a chained scatterlist, it could jump to the start of a new
104  *   scatterlist array.
105  *
106  **/
sg_next(struct scatterlist * sg)107 static inline struct scatterlist *sg_next(struct scatterlist *sg)
108 {
109 	if (sg_is_last(sg))
110 		return NULL;
111 
112 	sg++;
113 	if (unlikely(sg_is_chain(sg)))
114 		sg = sg_chain_ptr(sg);
115 
116 	return sg;
117 }
118 
119 /**
120  * sg_assign_page - Assign a given page to an SG entry
121  * @sg:		    SG entry
122  * @page:	    The page
123  *
124  * Description:
125  *   Assign page to sg entry. Also see sg_set_page(), the most commonly used
126  *   variant.
127  *
128  **/
sg_assign_page(struct scatterlist * sg,struct page * page)129 static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
130 {
131 	unsigned long page_link = sg->page_link & (SG_CHAIN | SG_END);
132 
133 	/*
134 	 * In order for the low bit stealing approach to work, pages
135 	 * must be aligned at a 32-bit boundary as a minimum.
136 	 */
137 	BUG_ON((unsigned long)page & SG_PAGE_LINK_MASK);
138 #ifdef CONFIG_DEBUG_SG
139 	BUG_ON(sg_is_chain(sg));
140 #endif
141 	sg->page_link = page_link | (unsigned long) page;
142 }
143 
144 /**
145  * sg_set_page - Set sg entry to point at given page
146  * @sg:		 SG entry
147  * @page:	 The page
148  * @len:	 Length of data
149  * @offset:	 Offset into page
150  *
151  * Description:
152  *   Use this function to set an sg entry pointing at a page, never assign
153  *   the page directly. We encode sg table information in the lower bits
154  *   of the page pointer. See sg_page() for looking up the page belonging
155  *   to an sg entry.
156  *
157  **/
sg_set_page(struct scatterlist * sg,struct page * page,unsigned int len,unsigned int offset)158 static inline void sg_set_page(struct scatterlist *sg, struct page *page,
159 			       unsigned int len, unsigned int offset)
160 {
161 	sg_assign_page(sg, page);
162 	sg->offset = offset;
163 	sg->length = len;
164 }
165 
166 /**
167  * sg_set_folio - Set sg entry to point at given folio
168  * @sg:		 SG entry
169  * @folio:	 The folio
170  * @len:	 Length of data
171  * @offset:	 Offset into folio
172  *
173  * Description:
174  *   Use this function to set an sg entry pointing at a folio, never assign
175  *   the folio directly. We encode sg table information in the lower bits
176  *   of the folio pointer. See sg_page() for looking up the page belonging
177  *   to an sg entry.
178  *
179  **/
sg_set_folio(struct scatterlist * sg,struct folio * folio,size_t len,size_t offset)180 static inline void sg_set_folio(struct scatterlist *sg, struct folio *folio,
181 			       size_t len, size_t offset)
182 {
183 	WARN_ON_ONCE(len > UINT_MAX);
184 	WARN_ON_ONCE(offset > UINT_MAX);
185 	sg_assign_page(sg, &folio->page);
186 	sg->offset = offset;
187 	sg->length = len;
188 }
189 
sg_page(struct scatterlist * sg)190 static inline struct page *sg_page(struct scatterlist *sg)
191 {
192 #ifdef CONFIG_DEBUG_SG
193 	BUG_ON(sg_is_chain(sg));
194 #endif
195 	return (struct page *)((sg)->page_link & ~SG_PAGE_LINK_MASK);
196 }
197 
198 /**
199  * sg_set_buf - Set sg entry to point at given data
200  * @sg:		 SG entry
201  * @buf:	 Data
202  * @buflen:	 Data length
203  *
204  **/
sg_set_buf(struct scatterlist * sg,const void * buf,unsigned int buflen)205 static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
206 			      unsigned int buflen)
207 {
208 #ifdef CONFIG_DEBUG_SG
209 	BUG_ON(!virt_addr_valid(buf));
210 #endif
211 	sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
212 }
213 
214 /*
215  * Loop over each sg element, following the pointer to a new list if necessary
216  */
217 #define for_each_sg(sglist, sg, nr, __i)	\
218 	for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
219 
220 /*
221  * Loop over each sg element in the given sg_table object.
222  */
223 #define for_each_sgtable_sg(sgt, sg, i)		\
224 	for_each_sg((sgt)->sgl, sg, (sgt)->orig_nents, i)
225 
226 /*
227  * Loop over each sg element in the given *DMA mapped* sg_table object.
228  * Please use sg_dma_address(sg) and sg_dma_len(sg) to extract DMA addresses
229  * of the each element.
230  */
231 #define for_each_sgtable_dma_sg(sgt, sg, i)	\
232 	for_each_sg((sgt)->sgl, sg, (sgt)->nents, i)
233 
__sg_chain(struct scatterlist * chain_sg,struct scatterlist * sgl)234 static inline void __sg_chain(struct scatterlist *chain_sg,
235 			      struct scatterlist *sgl)
236 {
237 	/*
238 	 * offset and length are unused for chain entry. Clear them.
239 	 */
240 	chain_sg->offset = 0;
241 	chain_sg->length = 0;
242 
243 	/*
244 	 * Set lowest bit to indicate a link pointer, and make sure to clear
245 	 * the termination bit if it happens to be set.
246 	 */
247 	chain_sg->page_link = ((unsigned long) sgl | SG_CHAIN) & ~SG_END;
248 }
249 
250 /**
251  * sg_chain - Chain two sglists together
252  * @prv:	First scatterlist
253  * @prv_nents:	Number of entries in prv
254  * @sgl:	Second scatterlist
255  *
256  * Description:
257  *   Links @prv and @sgl together, to form a longer scatterlist.
258  *
259  **/
sg_chain(struct scatterlist * prv,unsigned int prv_nents,struct scatterlist * sgl)260 static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents,
261 			    struct scatterlist *sgl)
262 {
263 	__sg_chain(&prv[prv_nents - 1], sgl);
264 }
265 
266 /**
267  * sg_mark_end - Mark the end of the scatterlist
268  * @sg:		 SG entryScatterlist
269  *
270  * Description:
271  *   Marks the passed in sg entry as the termination point for the sg
272  *   table. A call to sg_next() on this entry will return NULL.
273  *
274  **/
sg_mark_end(struct scatterlist * sg)275 static inline void sg_mark_end(struct scatterlist *sg)
276 {
277 	/*
278 	 * Set termination bit, clear potential chain bit
279 	 */
280 	sg->page_link |= SG_END;
281 	sg->page_link &= ~SG_CHAIN;
282 }
283 
284 /**
285  * sg_unmark_end - Undo setting the end of the scatterlist
286  * @sg:		 SG entryScatterlist
287  *
288  * Description:
289  *   Removes the termination marker from the given entry of the scatterlist.
290  *
291  **/
sg_unmark_end(struct scatterlist * sg)292 static inline void sg_unmark_end(struct scatterlist *sg)
293 {
294 	sg->page_link &= ~SG_END;
295 }
296 
297 /*
298  * On 64-bit architectures there is a 4-byte padding in struct scatterlist
299  * (assuming also CONFIG_NEED_SG_DMA_LENGTH is set). Use this padding for DMA
300  * flags bits to indicate when a specific dma address is a bus address or the
301  * buffer may have been bounced via SWIOTLB.
302  */
303 #ifdef CONFIG_NEED_SG_DMA_FLAGS
304 
305 #define SG_DMA_BUS_ADDRESS	(1 << 0)
306 #define SG_DMA_SWIOTLB		(1 << 1)
307 
308 /**
309  * sg_dma_is_bus_address - Return whether a given segment was marked
310  *			   as a bus address
311  * @sg:		 SG entry
312  *
313  * Description:
314  *   Returns true if sg_dma_mark_bus_address() has been called on
315  *   this segment.
316  **/
sg_dma_is_bus_address(struct scatterlist * sg)317 static inline bool sg_dma_is_bus_address(struct scatterlist *sg)
318 {
319 	return sg->dma_flags & SG_DMA_BUS_ADDRESS;
320 }
321 
322 /**
323  * sg_dma_mark_bus_address - Mark the scatterlist entry as a bus address
324  * @sg:		 SG entry
325  *
326  * Description:
327  *   Marks the passed in sg entry to indicate that the dma_address is
328  *   a bus address and doesn't need to be unmapped. This should only be
329  *   used by dma_map_sg() implementations to mark bus addresses
330  *   so they can be properly cleaned up in dma_unmap_sg().
331  **/
sg_dma_mark_bus_address(struct scatterlist * sg)332 static inline void sg_dma_mark_bus_address(struct scatterlist *sg)
333 {
334 	sg->dma_flags |= SG_DMA_BUS_ADDRESS;
335 }
336 
337 /**
338  * sg_dma_unmark_bus_address - Unmark the scatterlist entry as a bus address
339  * @sg:		 SG entry
340  *
341  * Description:
342  *   Clears the bus address mark.
343  **/
sg_dma_unmark_bus_address(struct scatterlist * sg)344 static inline void sg_dma_unmark_bus_address(struct scatterlist *sg)
345 {
346 	sg->dma_flags &= ~SG_DMA_BUS_ADDRESS;
347 }
348 
349 /**
350  * sg_dma_is_swiotlb - Return whether the scatterlist was marked for SWIOTLB
351  *			bouncing
352  * @sg:		SG entry
353  *
354  * Description:
355  *   Returns true if the scatterlist was marked for SWIOTLB bouncing. Not all
356  *   elements may have been bounced, so the caller would have to check
357  *   individual SG entries with swiotlb_find_pool().
358  */
sg_dma_is_swiotlb(struct scatterlist * sg)359 static inline bool sg_dma_is_swiotlb(struct scatterlist *sg)
360 {
361 	return sg->dma_flags & SG_DMA_SWIOTLB;
362 }
363 
364 /**
365  * sg_dma_mark_swiotlb - Mark the scatterlist for SWIOTLB bouncing
366  * @sg:		SG entry
367  *
368  * Description:
369  *   Marks a a scatterlist for SWIOTLB bounce. Not all SG entries may be
370  *   bounced.
371  */
sg_dma_mark_swiotlb(struct scatterlist * sg)372 static inline void sg_dma_mark_swiotlb(struct scatterlist *sg)
373 {
374 	sg->dma_flags |= SG_DMA_SWIOTLB;
375 }
376 
377 #else
378 
sg_dma_is_bus_address(struct scatterlist * sg)379 static inline bool sg_dma_is_bus_address(struct scatterlist *sg)
380 {
381 	return false;
382 }
sg_dma_mark_bus_address(struct scatterlist * sg)383 static inline void sg_dma_mark_bus_address(struct scatterlist *sg)
384 {
385 }
sg_dma_unmark_bus_address(struct scatterlist * sg)386 static inline void sg_dma_unmark_bus_address(struct scatterlist *sg)
387 {
388 }
sg_dma_is_swiotlb(struct scatterlist * sg)389 static inline bool sg_dma_is_swiotlb(struct scatterlist *sg)
390 {
391 	return false;
392 }
sg_dma_mark_swiotlb(struct scatterlist * sg)393 static inline void sg_dma_mark_swiotlb(struct scatterlist *sg)
394 {
395 }
396 
397 #endif	/* CONFIG_NEED_SG_DMA_FLAGS */
398 
399 /**
400  * sg_phys - Return physical address of an sg entry
401  * @sg:	     SG entry
402  *
403  * Description:
404  *   This calls page_to_phys() on the page in this sg entry, and adds the
405  *   sg offset. The caller must know that it is legal to call page_to_phys()
406  *   on the sg page.
407  *
408  **/
sg_phys(struct scatterlist * sg)409 static inline dma_addr_t sg_phys(struct scatterlist *sg)
410 {
411 	return page_to_phys(sg_page(sg)) + sg->offset;
412 }
413 
414 /**
415  * sg_virt - Return virtual address of an sg entry
416  * @sg:      SG entry
417  *
418  * Description:
419  *   This calls page_address() on the page in this sg entry, and adds the
420  *   sg offset. The caller must know that the sg page has a valid virtual
421  *   mapping.
422  *
423  **/
sg_virt(struct scatterlist * sg)424 static inline void *sg_virt(struct scatterlist *sg)
425 {
426 	return page_address(sg_page(sg)) + sg->offset;
427 }
428 
429 /**
430  * sg_init_marker - Initialize markers in sg table
431  * @sgl:	   The SG table
432  * @nents:	   Number of entries in table
433  *
434  **/
sg_init_marker(struct scatterlist * sgl,unsigned int nents)435 static inline void sg_init_marker(struct scatterlist *sgl,
436 				  unsigned int nents)
437 {
438 	sg_mark_end(&sgl[nents - 1]);
439 }
440 
441 int sg_nents(struct scatterlist *sg);
442 int sg_nents_for_len(struct scatterlist *sg, u64 len);
443 struct scatterlist *sg_last(struct scatterlist *s, unsigned int);
444 void sg_init_table(struct scatterlist *, unsigned int);
445 void sg_init_one(struct scatterlist *, const void *, unsigned int);
446 int sg_split(struct scatterlist *in, const int in_mapped_nents,
447 	     const off_t skip, const int nb_splits,
448 	     const size_t *split_sizes,
449 	     struct scatterlist **out, int *out_mapped_nents,
450 	     gfp_t gfp_mask);
451 
452 typedef struct scatterlist *(sg_alloc_fn)(unsigned int, gfp_t);
453 typedef void (sg_free_fn)(struct scatterlist *, unsigned int);
454 
455 void __sg_free_table(struct sg_table *, unsigned int, unsigned int,
456 		     sg_free_fn *, unsigned int);
457 void sg_free_table(struct sg_table *);
458 void sg_free_append_table(struct sg_append_table *sgt);
459 int __sg_alloc_table(struct sg_table *, unsigned int, unsigned int,
460 		     struct scatterlist *, unsigned int, gfp_t, sg_alloc_fn *);
461 int sg_alloc_table(struct sg_table *, unsigned int, gfp_t);
462 int sg_alloc_append_table_from_pages(struct sg_append_table *sgt,
463 				     struct page **pages, unsigned int n_pages,
464 				     unsigned int offset, unsigned long size,
465 				     unsigned int max_segment,
466 				     unsigned int left_pages, gfp_t gfp_mask);
467 int sg_alloc_table_from_pages_segment(struct sg_table *sgt, struct page **pages,
468 				      unsigned int n_pages, unsigned int offset,
469 				      unsigned long size,
470 				      unsigned int max_segment, gfp_t gfp_mask);
471 
472 /**
473  * sg_alloc_table_from_pages - Allocate and initialize an sg table from
474  *			       an array of pages
475  * @sgt:	 The sg table header to use
476  * @pages:	 Pointer to an array of page pointers
477  * @n_pages:	 Number of pages in the pages array
478  * @offset:      Offset from start of the first page to the start of a buffer
479  * @size:        Number of valid bytes in the buffer (after offset)
480  * @gfp_mask:	 GFP allocation mask
481  *
482  *  Description:
483  *    Allocate and initialize an sg table from a list of pages. Contiguous
484  *    ranges of the pages are squashed into a single scatterlist node. A user
485  *    may provide an offset at a start and a size of valid data in a buffer
486  *    specified by the page array. The returned sg table is released by
487  *    sg_free_table.
488  *
489  * Returns:
490  *   0 on success, negative error on failure
491  */
sg_alloc_table_from_pages(struct sg_table * sgt,struct page ** pages,unsigned int n_pages,unsigned int offset,unsigned long size,gfp_t gfp_mask)492 static inline int sg_alloc_table_from_pages(struct sg_table *sgt,
493 					    struct page **pages,
494 					    unsigned int n_pages,
495 					    unsigned int offset,
496 					    unsigned long size, gfp_t gfp_mask)
497 {
498 	return sg_alloc_table_from_pages_segment(sgt, pages, n_pages, offset,
499 						 size, UINT_MAX, gfp_mask);
500 }
501 
502 #ifdef CONFIG_SGL_ALLOC
503 struct scatterlist *sgl_alloc_order(unsigned long long length,
504 				    unsigned int order, bool chainable,
505 				    gfp_t gfp, unsigned int *nent_p);
506 struct scatterlist *sgl_alloc(unsigned long long length, gfp_t gfp,
507 			      unsigned int *nent_p);
508 void sgl_free_n_order(struct scatterlist *sgl, int nents, int order);
509 void sgl_free_order(struct scatterlist *sgl, int order);
510 void sgl_free(struct scatterlist *sgl);
511 #endif /* CONFIG_SGL_ALLOC */
512 
513 size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf,
514 		      size_t buflen, off_t skip, bool to_buffer);
515 
516 size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
517 			   const void *buf, size_t buflen);
518 size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
519 			 void *buf, size_t buflen);
520 
521 size_t sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents,
522 			    const void *buf, size_t buflen, off_t skip);
523 size_t sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents,
524 			  void *buf, size_t buflen, off_t skip);
525 size_t sg_zero_buffer(struct scatterlist *sgl, unsigned int nents,
526 		       size_t buflen, off_t skip);
527 
528 /*
529  * Maximum number of entries that will be allocated in one piece, if
530  * a list larger than this is required then chaining will be utilized.
531  */
532 #define SG_MAX_SINGLE_ALLOC		(PAGE_SIZE / sizeof(struct scatterlist))
533 
534 /*
535  * The maximum number of SG segments that we will put inside a
536  * scatterlist (unless chaining is used). Should ideally fit inside a
537  * single page, to avoid a higher order allocation.  We could define this
538  * to SG_MAX_SINGLE_ALLOC to pack correctly at the highest order.  The
539  * minimum value is 32
540  */
541 #define SG_CHUNK_SIZE	128
542 
543 /*
544  * Like SG_CHUNK_SIZE, but for archs that have sg chaining. This limit
545  * is totally arbitrary, a setting of 2048 will get you at least 8mb ios.
546  */
547 #ifdef CONFIG_ARCH_NO_SG_CHAIN
548 #define SG_MAX_SEGMENTS	SG_CHUNK_SIZE
549 #else
550 #define SG_MAX_SEGMENTS	2048
551 #endif
552 
553 #ifdef CONFIG_SG_POOL
554 void sg_free_table_chained(struct sg_table *table,
555 			   unsigned nents_first_chunk);
556 int sg_alloc_table_chained(struct sg_table *table, int nents,
557 			   struct scatterlist *first_chunk,
558 			   unsigned nents_first_chunk);
559 #endif
560 
561 /*
562  * sg page iterator
563  *
564  * Iterates over sg entries page-by-page.  On each successful iteration, you
565  * can call sg_page_iter_page(@piter) to get the current page.
566  * @piter->sg will point to the sg holding this page and @piter->sg_pgoffset to
567  * the page's page offset within the sg. The iteration will stop either when a
568  * maximum number of sg entries was reached or a terminating sg
569  * (sg_last(sg) == true) was reached.
570  */
571 struct sg_page_iter {
572 	struct scatterlist	*sg;		/* sg holding the page */
573 	unsigned int		sg_pgoffset;	/* page offset within the sg */
574 
575 	/* these are internal states, keep away */
576 	unsigned int		__nents;	/* remaining sg entries */
577 	int			__pg_advance;	/* nr pages to advance at the
578 						 * next step */
579 };
580 
581 /*
582  * sg page iterator for DMA addresses
583  *
584  * This is the same as sg_page_iter however you can call
585  * sg_page_iter_dma_address(@dma_iter) to get the page's DMA
586  * address. sg_page_iter_page() cannot be called on this iterator.
587  */
588 struct sg_dma_page_iter {
589 	struct sg_page_iter base;
590 };
591 
592 bool __sg_page_iter_next(struct sg_page_iter *piter);
593 bool __sg_page_iter_dma_next(struct sg_dma_page_iter *dma_iter);
594 void __sg_page_iter_start(struct sg_page_iter *piter,
595 			  struct scatterlist *sglist, unsigned int nents,
596 			  unsigned long pgoffset);
597 /**
598  * sg_page_iter_page - get the current page held by the page iterator
599  * @piter:	page iterator holding the page
600  */
sg_page_iter_page(struct sg_page_iter * piter)601 static inline struct page *sg_page_iter_page(struct sg_page_iter *piter)
602 {
603 	return nth_page(sg_page(piter->sg), piter->sg_pgoffset);
604 }
605 
606 /**
607  * sg_page_iter_dma_address - get the dma address of the current page held by
608  * the page iterator.
609  * @dma_iter:	page iterator holding the page
610  */
611 static inline dma_addr_t
sg_page_iter_dma_address(struct sg_dma_page_iter * dma_iter)612 sg_page_iter_dma_address(struct sg_dma_page_iter *dma_iter)
613 {
614 	return sg_dma_address(dma_iter->base.sg) +
615 	       (dma_iter->base.sg_pgoffset << PAGE_SHIFT);
616 }
617 
618 /**
619  * for_each_sg_page - iterate over the pages of the given sg list
620  * @sglist:	sglist to iterate over
621  * @piter:	page iterator to hold current page, sg, sg_pgoffset
622  * @nents:	maximum number of sg entries to iterate over
623  * @pgoffset:	starting page offset (in pages)
624  *
625  * Callers may use sg_page_iter_page() to get each page pointer.
626  * In each loop it operates on PAGE_SIZE unit.
627  */
628 #define for_each_sg_page(sglist, piter, nents, pgoffset)		   \
629 	for (__sg_page_iter_start((piter), (sglist), (nents), (pgoffset)); \
630 	     __sg_page_iter_next(piter);)
631 
632 /**
633  * for_each_sg_dma_page - iterate over the pages of the given sg list
634  * @sglist:	sglist to iterate over
635  * @dma_iter:	DMA page iterator to hold current page
636  * @dma_nents:	maximum number of sg entries to iterate over, this is the value
637  *              returned from dma_map_sg
638  * @pgoffset:	starting page offset (in pages)
639  *
640  * Callers may use sg_page_iter_dma_address() to get each page's DMA address.
641  * In each loop it operates on PAGE_SIZE unit.
642  */
643 #define for_each_sg_dma_page(sglist, dma_iter, dma_nents, pgoffset)            \
644 	for (__sg_page_iter_start(&(dma_iter)->base, sglist, dma_nents,        \
645 				  pgoffset);                                   \
646 	     __sg_page_iter_dma_next(dma_iter);)
647 
648 /**
649  * for_each_sgtable_page - iterate over all pages in the sg_table object
650  * @sgt:	sg_table object to iterate over
651  * @piter:	page iterator to hold current page
652  * @pgoffset:	starting page offset (in pages)
653  *
654  * Iterates over the all memory pages in the buffer described by
655  * a scatterlist stored in the given sg_table object.
656  * See also for_each_sg_page(). In each loop it operates on PAGE_SIZE unit.
657  */
658 #define for_each_sgtable_page(sgt, piter, pgoffset)	\
659 	for_each_sg_page((sgt)->sgl, piter, (sgt)->orig_nents, pgoffset)
660 
661 /**
662  * for_each_sgtable_dma_page - iterate over the DMA mapped sg_table object
663  * @sgt:	sg_table object to iterate over
664  * @dma_iter:	DMA page iterator to hold current page
665  * @pgoffset:	starting page offset (in pages)
666  *
667  * Iterates over the all DMA mapped pages in the buffer described by
668  * a scatterlist stored in the given sg_table object.
669  * See also for_each_sg_dma_page(). In each loop it operates on PAGE_SIZE
670  * unit.
671  */
672 #define for_each_sgtable_dma_page(sgt, dma_iter, pgoffset)	\
673 	for_each_sg_dma_page((sgt)->sgl, dma_iter, (sgt)->nents, pgoffset)
674 
675 
676 /*
677  * Mapping sg iterator
678  *
679  * Iterates over sg entries mapping page-by-page.  On each successful
680  * iteration, @miter->page points to the mapped page and
681  * @miter->length bytes of data can be accessed at @miter->addr.  As
682  * long as an iteration is enclosed between start and stop, the user
683  * is free to choose control structure and when to stop.
684  *
685  * @miter->consumed is set to @miter->length on each iteration.  It
686  * can be adjusted if the user can't consume all the bytes in one go.
687  * Also, a stopped iteration can be resumed by calling next on it.
688  * This is useful when iteration needs to release all resources and
689  * continue later (e.g. at the next interrupt).
690  */
691 
692 #define SG_MITER_ATOMIC		(1 << 0)	 /* use kmap_atomic */
693 #define SG_MITER_TO_SG		(1 << 1)	/* flush back to phys on unmap */
694 #define SG_MITER_FROM_SG	(1 << 2)	/* nop */
695 #define SG_MITER_LOCAL		(1 << 3)	 /* use kmap_local */
696 
697 struct sg_mapping_iter {
698 	/* the following three fields can be accessed directly */
699 	struct page		*page;		/* currently mapped page */
700 	void			*addr;		/* pointer to the mapped area */
701 	size_t			length;		/* length of the mapped area */
702 	size_t			consumed;	/* number of consumed bytes */
703 	struct sg_page_iter	piter;		/* page iterator */
704 
705 	/* these are internal states, keep away */
706 	unsigned int		__offset;	/* offset within page */
707 	unsigned int		__remaining;	/* remaining bytes on page */
708 	unsigned int		__flags;
709 };
710 
711 void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
712 		    unsigned int nents, unsigned int flags);
713 bool sg_miter_skip(struct sg_mapping_iter *miter, off_t offset);
714 bool sg_miter_next(struct sg_mapping_iter *miter);
715 void sg_miter_stop(struct sg_mapping_iter *miter);
716 
717 #endif /* _LINUX_SCATTERLIST_H */
718