File Index
Files and directories
Sign in
FILE

scatterlist.h

File preview with syntax highlighting.

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