Blame view

include/linux/scatterlist.h 16.3 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  /* SPDX-License-Identifier: GPL-2.0 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
  #ifndef _LINUX_SCATTERLIST_H
  #define _LINUX_SCATTERLIST_H
187f1882b   Paul Gortmaker   BUG: headers with...
4
  #include <linux/string.h>
84be456f8   Christoph Hellwig   remove <asm/scatt...
5
  #include <linux/types.h>
187f1882b   Paul Gortmaker   BUG: headers with...
6
7
  #include <linux/bug.h>
  #include <linux/mm.h>
18dabf473   Jens Axboe   Change table chai...
8
  #include <asm/io.h>
84be456f8   Christoph Hellwig   remove <asm/scatt...
9
  struct scatterlist {
84be456f8   Christoph Hellwig   remove <asm/scatt...
10
11
12
13
14
15
16
17
18
19
  	unsigned long	page_link;
  	unsigned int	offset;
  	unsigned int	length;
  	dma_addr_t	dma_address;
  #ifdef CONFIG_NEED_SG_DMA_LENGTH
  	unsigned int	dma_length;
  #endif
  };
  
  /*
c125906b8   Tvrtko Ursulin   lib/scatterlist: ...
20
21
22
23
24
25
   * Since the above length field is an unsigned int, below we define the maximum
   * length in bytes that can be stored in one scatterlist entry.
   */
  #define SCATTERLIST_MAX_SEGMENT (UINT_MAX & PAGE_MASK)
  
  /*
84be456f8   Christoph Hellwig   remove <asm/scatt...
26
27
28
29
30
31
32
33
34
35
36
37
38
   * These macros should be used after a dma_map_sg call has been done
   * to get bus addresses of each of the SG entries and their lengths.
   * You should only work with the number of sg entries dma_map_sg
   * returns, or alternatively stop on the first sg_dma_len(sg) which
   * is 0.
   */
  #define sg_dma_address(sg)	((sg)->dma_address)
  
  #ifdef CONFIG_NEED_SG_DMA_LENGTH
  #define sg_dma_len(sg)		((sg)->dma_length)
  #else
  #define sg_dma_len(sg)		((sg)->length)
  #endif
0db9299f4   Jens Axboe   SG: Move function...
39
40
41
42
43
  struct sg_table {
  	struct scatterlist *sgl;	/* the list */
  	unsigned int nents;		/* number of mapped entries */
  	unsigned int orig_nents;	/* original size of list */
  };
18dabf473   Jens Axboe   Change table chai...
44
45
46
  /*
   * Notes on SG table design.
   *
84be456f8   Christoph Hellwig   remove <asm/scatt...
47
48
49
   * We use the unsigned long page_link field in the scatterlist struct to place
   * the page pointer AND encode information about the sg table as well. The two
   * lower bits are reserved for this information.
18dabf473   Jens Axboe   Change table chai...
50
51
52
53
54
55
56
57
58
   *
   * If bit 0 is set, then the page_link contains a pointer to the next sg
   * table list. Otherwise the next entry is at sg + 1.
   *
   * If bit 1 is set, then this sg entry is the last element in a list.
   *
   * See sg_next().
   *
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59

723fbf563   Anshuman Khandual   lib/scatterlist: ...
60
61
  #define SG_CHAIN	0x01UL
  #define SG_END		0x02UL
d6ec08420   Jens Axboe   Add CONFIG_DEBUG_...
62

645a8d946   Tejun Heo   scatterlist: add ...
63
64
65
66
67
  /*
   * We overload the LSB of the page pointer to indicate whether it's
   * a valid sg entry, or whether it points to the start of a new scatterlist.
   * Those low bits are there for everyone! (thanks mason :-)
   */
723fbf563   Anshuman Khandual   lib/scatterlist: ...
68
69
  #define sg_is_chain(sg)		((sg)->page_link & SG_CHAIN)
  #define sg_is_last(sg)		((sg)->page_link & SG_END)
645a8d946   Tejun Heo   scatterlist: add ...
70
  #define sg_chain_ptr(sg)	\
723fbf563   Anshuman Khandual   lib/scatterlist: ...
71
  	((struct scatterlist *) ((sg)->page_link & ~(SG_CHAIN | SG_END)))
645a8d946   Tejun Heo   scatterlist: add ...
72

82f66fbef   Jens Axboe   [SG] Add helpers ...
73
  /**
642f14903   Jens Axboe   SG: Change sg_set...
74
75
76
   * sg_assign_page - Assign a given page to an SG entry
   * @sg:		    SG entry
   * @page:	    The page
82f66fbef   Jens Axboe   [SG] Add helpers ...
77
78
   *
   * Description:
642f14903   Jens Axboe   SG: Change sg_set...
79
80
   *   Assign page to sg entry. Also see sg_set_page(), the most commonly used
   *   variant.
82f66fbef   Jens Axboe   [SG] Add helpers ...
81
82
   *
   **/
642f14903   Jens Axboe   SG: Change sg_set...
83
  static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
82f66fbef   Jens Axboe   [SG] Add helpers ...
84
  {
723fbf563   Anshuman Khandual   lib/scatterlist: ...
85
  	unsigned long page_link = sg->page_link & (SG_CHAIN | SG_END);
18dabf473   Jens Axboe   Change table chai...
86

de26103de   Jens Axboe   [SG] Add debug ch...
87
88
89
90
  	/*
  	 * In order for the low bit stealing approach to work, pages
  	 * must be aligned at a 32-bit boundary as a minimum.
  	 */
723fbf563   Anshuman Khandual   lib/scatterlist: ...
91
  	BUG_ON((unsigned long) page & (SG_CHAIN | SG_END));
d6ec08420   Jens Axboe   Add CONFIG_DEBUG_...
92
  #ifdef CONFIG_DEBUG_SG
645a8d946   Tejun Heo   scatterlist: add ...
93
  	BUG_ON(sg_is_chain(sg));
d6ec08420   Jens Axboe   Add CONFIG_DEBUG_...
94
  #endif
18dabf473   Jens Axboe   Change table chai...
95
  	sg->page_link = page_link | (unsigned long) page;
82f66fbef   Jens Axboe   [SG] Add helpers ...
96
  }
642f14903   Jens Axboe   SG: Change sg_set...
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  /**
   * sg_set_page - Set sg entry to point at given page
   * @sg:		 SG entry
   * @page:	 The page
   * @len:	 Length of data
   * @offset:	 Offset into page
   *
   * Description:
   *   Use this function to set an sg entry pointing at a page, never assign
   *   the page directly. We encode sg table information in the lower bits
   *   of the page pointer. See sg_page() for looking up the page belonging
   *   to an sg entry.
   *
   **/
  static inline void sg_set_page(struct scatterlist *sg, struct page *page,
  			       unsigned int len, unsigned int offset)
  {
  	sg_assign_page(sg, page);
  	sg->offset = offset;
  	sg->length = len;
  }
645a8d946   Tejun Heo   scatterlist: add ...
118
119
120
  static inline struct page *sg_page(struct scatterlist *sg)
  {
  #ifdef CONFIG_DEBUG_SG
645a8d946   Tejun Heo   scatterlist: add ...
121
122
  	BUG_ON(sg_is_chain(sg));
  #endif
723fbf563   Anshuman Khandual   lib/scatterlist: ...
123
  	return (struct page *)((sg)->page_link & ~(SG_CHAIN | SG_END));
645a8d946   Tejun Heo   scatterlist: add ...
124
  }
82f66fbef   Jens Axboe   [SG] Add helpers ...
125

18dabf473   Jens Axboe   Change table chai...
126
127
128
129
130
131
132
  /**
   * sg_set_buf - Set sg entry to point at given data
   * @sg:		 SG entry
   * @buf:	 Data
   * @buflen:	 Data length
   *
   **/
03fd9cee7   Herbert Xu   [PATCH] scatterli...
133
  static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
d32311fed   Herbert Xu   [PATCH] Introduce...
134
135
  			      unsigned int buflen)
  {
ac4e97abc   Rusty Russell   scatterlist: sg_s...
136
137
138
  #ifdef CONFIG_DEBUG_SG
  	BUG_ON(!virt_addr_valid(buf));
  #endif
642f14903   Jens Axboe   SG: Change sg_set...
139
  	sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
140
  }
96b418c96   Jens Axboe   Add sg helpers fo...
141
142
143
144
145
  /*
   * Loop over each sg element, following the pointer to a new list if necessary
   */
  #define for_each_sg(sglist, sg, nr, __i)	\
  	for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
709d6d73c   Marek Szyprowski   scatterlist: add ...
146
147
148
149
  /*
   * Loop over each sg element in the given sg_table object.
   */
  #define for_each_sgtable_sg(sgt, sg, i)		\
68d237056   Marek Szyprowski   scatterlist: prot...
150
  	for_each_sg((sgt)->sgl, sg, (sgt)->orig_nents, i)
709d6d73c   Marek Szyprowski   scatterlist: add ...
151
152
153
154
155
156
157
  
  /*
   * Loop over each sg element in the given *DMA mapped* sg_table object.
   * Please use sg_dma_address(sg) and sg_dma_len(sg) to extract DMA addresses
   * of the each element.
   */
  #define for_each_sgtable_dma_sg(sgt, sg, i)	\
68d237056   Marek Szyprowski   scatterlist: prot...
158
  	for_each_sg((sgt)->sgl, sg, (sgt)->nents, i)
709d6d73c   Marek Szyprowski   scatterlist: add ...
159

07da1223e   Maor Gottlieb   lib/scatterlist: ...
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  static inline void __sg_chain(struct scatterlist *chain_sg,
  			      struct scatterlist *sgl)
  {
  	/*
  	 * offset and length are unused for chain entry. Clear them.
  	 */
  	chain_sg->offset = 0;
  	chain_sg->length = 0;
  
  	/*
  	 * Set lowest bit to indicate a link pointer, and make sure to clear
  	 * the termination bit if it happens to be set.
  	 */
  	chain_sg->page_link = ((unsigned long) sgl | SG_CHAIN) & ~SG_END;
  }
70eb8040d   Jens Axboe   Add chained sg su...
175
  /**
70eb8040d   Jens Axboe   Add chained sg su...
176
177
178
179
180
   * sg_chain - Chain two sglists together
   * @prv:	First scatterlist
   * @prv_nents:	Number of entries in prv
   * @sgl:	Second scatterlist
   *
18dabf473   Jens Axboe   Change table chai...
181
182
   * Description:
   *   Links @prv@ and @sgl@ together, to form a longer scatterlist.
70eb8040d   Jens Axboe   Add chained sg su...
183
   *
18dabf473   Jens Axboe   Change table chai...
184
   **/
70eb8040d   Jens Axboe   Add chained sg su...
185
186
187
  static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents,
  			    struct scatterlist *sgl)
  {
07da1223e   Maor Gottlieb   lib/scatterlist: ...
188
  	__sg_chain(&prv[prv_nents - 1], sgl);
70eb8040d   Jens Axboe   Add chained sg su...
189
  }
82f66fbef   Jens Axboe   [SG] Add helpers ...
190
191
  /**
   * sg_mark_end - Mark the end of the scatterlist
c46f2334c   Jens Axboe   [SG] Get rid of _...
192
   * @sg:		 SG entryScatterlist
82f66fbef   Jens Axboe   [SG] Add helpers ...
193
194
   *
   * Description:
c46f2334c   Jens Axboe   [SG] Get rid of _...
195
196
   *   Marks the passed in sg entry as the termination point for the sg
   *   table. A call to sg_next() on this entry will return NULL.
82f66fbef   Jens Axboe   [SG] Add helpers ...
197
198
   *
   **/
c46f2334c   Jens Axboe   [SG] Get rid of _...
199
  static inline void sg_mark_end(struct scatterlist *sg)
82f66fbef   Jens Axboe   [SG] Add helpers ...
200
  {
c46f2334c   Jens Axboe   [SG] Get rid of _...
201
202
203
  	/*
  	 * Set termination bit, clear potential chain bit
  	 */
723fbf563   Anshuman Khandual   lib/scatterlist: ...
204
205
  	sg->page_link |= SG_END;
  	sg->page_link &= ~SG_CHAIN;
82f66fbef   Jens Axboe   [SG] Add helpers ...
206
  }
82f66fbef   Jens Axboe   [SG] Add helpers ...
207
  /**
c8164d893   Paolo Bonzini   scatterlist: intr...
208
209
210
211
212
213
214
215
216
   * sg_unmark_end - Undo setting the end of the scatterlist
   * @sg:		 SG entryScatterlist
   *
   * Description:
   *   Removes the termination marker from the given entry of the scatterlist.
   *
   **/
  static inline void sg_unmark_end(struct scatterlist *sg)
  {
723fbf563   Anshuman Khandual   lib/scatterlist: ...
217
  	sg->page_link &= ~SG_END;
c8164d893   Paolo Bonzini   scatterlist: intr...
218
219
220
  }
  
  /**
82f66fbef   Jens Axboe   [SG] Add helpers ...
221
222
223
224
225
226
227
228
229
   * sg_phys - Return physical address of an sg entry
   * @sg:	     SG entry
   *
   * Description:
   *   This calls page_to_phys() on the page in this sg entry, and adds the
   *   sg offset. The caller must know that it is legal to call page_to_phys()
   *   on the sg page.
   *
   **/
85cdffcde   Hugh Dickins   fix sg_phys to us...
230
  static inline dma_addr_t sg_phys(struct scatterlist *sg)
82f66fbef   Jens Axboe   [SG] Add helpers ...
231
232
233
234
235
236
  {
  	return page_to_phys(sg_page(sg)) + sg->offset;
  }
  
  /**
   * sg_virt - Return virtual address of an sg entry
18dabf473   Jens Axboe   Change table chai...
237
   * @sg:      SG entry
82f66fbef   Jens Axboe   [SG] Add helpers ...
238
239
240
241
242
243
244
245
246
247
248
   *
   * Description:
   *   This calls page_address() on the page in this sg entry, and adds the
   *   sg offset. The caller must know that the sg page has a valid virtual
   *   mapping.
   *
   **/
  static inline void *sg_virt(struct scatterlist *sg)
  {
  	return page_address(sg_page(sg)) + sg->offset;
  }
f38517867   Prashant Bhole   lib/scatterlist: ...
249
250
251
252
253
254
255
256
257
  /**
   * sg_init_marker - Initialize markers in sg table
   * @sgl:	   The SG table
   * @nents:	   Number of entries in table
   *
   **/
  static inline void sg_init_marker(struct scatterlist *sgl,
  				  unsigned int nents)
  {
f38517867   Prashant Bhole   lib/scatterlist: ...
258
259
  	sg_mark_end(&sgl[nents - 1]);
  }
2e4846102   Maxim Levitsky   scatterlist: add ...
260
  int sg_nents(struct scatterlist *sg);
cfaed10d1   Tom Lendacky   scatterlist: intr...
261
  int sg_nents_for_len(struct scatterlist *sg, u64 len);
0db9299f4   Jens Axboe   SG: Move function...
262
263
264
265
  struct scatterlist *sg_next(struct scatterlist *);
  struct scatterlist *sg_last(struct scatterlist *s, unsigned int);
  void sg_init_table(struct scatterlist *, unsigned int);
  void sg_init_one(struct scatterlist *, const void *, unsigned int);
f8bcbe62a   Robert Jarzmik   lib: scatterlist:...
266
267
268
269
270
  int sg_split(struct scatterlist *in, const int in_mapped_nents,
  	     const off_t skip, const int nb_splits,
  	     const size_t *split_sizes,
  	     struct scatterlist **out, int *out_mapped_nents,
  	     gfp_t gfp_mask);
0db9299f4   Jens Axboe   SG: Move function...
271
272
273
  
  typedef struct scatterlist *(sg_alloc_fn)(unsigned int, gfp_t);
  typedef void (sg_free_fn)(struct scatterlist *, unsigned int);
4635873c5   Ming Lei   scsi: lib/sg_pool...
274
275
  void __sg_free_table(struct sg_table *, unsigned int, unsigned int,
  		     sg_free_fn *);
0db9299f4   Jens Axboe   SG: Move function...
276
  void sg_free_table(struct sg_table *);
c53c6d6a6   Christoph Hellwig   scatterlist: allo...
277
  int __sg_alloc_table(struct sg_table *, unsigned int, unsigned int,
4635873c5   Ming Lei   scsi: lib/sg_pool...
278
  		     struct scatterlist *, unsigned int, gfp_t, sg_alloc_fn *);
0db9299f4   Jens Axboe   SG: Move function...
279
  int sg_alloc_table(struct sg_table *, unsigned int, gfp_t);
07da1223e   Maor Gottlieb   lib/scatterlist: ...
280
281
282
283
284
  struct scatterlist *__sg_alloc_table_from_pages(struct sg_table *sgt,
  		struct page **pages, unsigned int n_pages, unsigned int offset,
  		unsigned long size, unsigned int max_segment,
  		struct scatterlist *prv, unsigned int left_pages,
  		gfp_t gfp_mask);
89d8589cd   Tvrtko Ursulin   lib/scatterlist: ...
285
286
287
  int 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);
0db9299f4   Jens Axboe   SG: Move function...
288

e80a0af47   Bart Van Assche   lib/scatterlist: ...
289
290
291
292
293
294
  #ifdef CONFIG_SGL_ALLOC
  struct scatterlist *sgl_alloc_order(unsigned long long length,
  				    unsigned int order, bool chainable,
  				    gfp_t gfp, unsigned int *nent_p);
  struct scatterlist *sgl_alloc(unsigned long long length, gfp_t gfp,
  			      unsigned int *nent_p);
8c7a8d1c4   Bart Van Assche   lib/scatterlist: ...
295
  void sgl_free_n_order(struct scatterlist *sgl, int nents, int order);
e80a0af47   Bart Van Assche   lib/scatterlist: ...
296
297
298
  void sgl_free_order(struct scatterlist *sgl, int order);
  void sgl_free(struct scatterlist *sgl);
  #endif /* CONFIG_SGL_ALLOC */
386ecb121   Dave Gordon   drivers/scsi/scsi...
299
300
  size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf,
  		      size_t buflen, off_t skip, bool to_buffer);
b1adaf65b   FUJITA Tomonori   [SCSI] block: add...
301
  size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
2a1bf8f93   Dave Gordon   lib/scatterlist: ...
302
  			   const void *buf, size_t buflen);
b1adaf65b   FUJITA Tomonori   [SCSI] block: add...
303
304
  size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
  			 void *buf, size_t buflen);
df642cea2   Akinobu Mita   lib/scatterlist: ...
305
  size_t sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents,
2a1bf8f93   Dave Gordon   lib/scatterlist: ...
306
  			    const void *buf, size_t buflen, off_t skip);
df642cea2   Akinobu Mita   lib/scatterlist: ...
307
308
  size_t sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents,
  			  void *buf, size_t buflen, off_t skip);
0945e5699   Johannes Thumshirn   scatterlist: add ...
309
310
  size_t sg_zero_buffer(struct scatterlist *sgl, unsigned int nents,
  		       size_t buflen, off_t skip);
df642cea2   Akinobu Mita   lib/scatterlist: ...
311

0db9299f4   Jens Axboe   SG: Move function...
312
313
314
315
316
  /*
   * Maximum number of entries that will be allocated in one piece, if
   * a list larger than this is required then chaining will be utilized.
   */
  #define SG_MAX_SINGLE_ALLOC		(PAGE_SIZE / sizeof(struct scatterlist))
a321e91b6   Imre Deak   lib/scatterlist: ...
317
  /*
9b1d6c895   Ming Lin   lib: scatterlist:...
318
319
320
321
322
323
324
325
326
327
328
329
   * The maximum number of SG segments that we will put inside a
   * scatterlist (unless chaining is used). Should ideally fit inside a
   * single page, to avoid a higher order allocation.  We could define this
   * to SG_MAX_SINGLE_ALLOC to pack correctly at the highest order.  The
   * minimum value is 32
   */
  #define SG_CHUNK_SIZE	128
  
  /*
   * Like SG_CHUNK_SIZE, but for archs that have sg chaining. This limit
   * is totally arbitrary, a setting of 2048 will get you at least 8mb ios.
   */
7c703e54c   Christoph Hellwig   arch: switch the ...
330
  #ifdef CONFIG_ARCH_NO_SG_CHAIN
9b1d6c895   Ming Lin   lib: scatterlist:...
331
  #define SG_MAX_SEGMENTS	SG_CHUNK_SIZE
7c703e54c   Christoph Hellwig   arch: switch the ...
332
333
  #else
  #define SG_MAX_SEGMENTS	2048
9b1d6c895   Ming Lin   lib: scatterlist:...
334
335
336
  #endif
  
  #ifdef CONFIG_SG_POOL
4635873c5   Ming Lei   scsi: lib/sg_pool...
337
338
  void sg_free_table_chained(struct sg_table *table,
  			   unsigned nents_first_chunk);
9b1d6c895   Ming Lin   lib: scatterlist:...
339
  int sg_alloc_table_chained(struct sg_table *table, int nents,
4635873c5   Ming Lei   scsi: lib/sg_pool...
340
341
  			   struct scatterlist *first_chunk,
  			   unsigned nents_first_chunk);
9b1d6c895   Ming Lin   lib: scatterlist:...
342
343
344
  #endif
  
  /*
a321e91b6   Imre Deak   lib/scatterlist: ...
345
346
   * sg page iterator
   *
d901b2760   Jason Gunthorpe   lib/scatterlist: ...
347
   * Iterates over sg entries page-by-page.  On each successful iteration, you
d2c4ada1e   Gal Pressman   lib/scatterlist: ...
348
349
350
351
352
   * can call sg_page_iter_page(@piter) to get the current page.
   * @piter->sg will point to the sg holding this page and @piter->sg_pgoffset to
   * the page's page offset within the sg. The iteration will stop either when a
   * maximum number of sg entries was reached or a terminating sg
   * (sg_last(sg) == true) was reached.
a321e91b6   Imre Deak   lib/scatterlist: ...
353
354
   */
  struct sg_page_iter {
a321e91b6   Imre Deak   lib/scatterlist: ...
355
356
357
358
359
360
361
362
  	struct scatterlist	*sg;		/* sg holding the page */
  	unsigned int		sg_pgoffset;	/* page offset within the sg */
  
  	/* these are internal states, keep away */
  	unsigned int		__nents;	/* remaining sg entries */
  	int			__pg_advance;	/* nr pages to advance at the
  						 * next step */
  };
d901b2760   Jason Gunthorpe   lib/scatterlist: ...
363
364
365
366
367
368
369
370
371
372
  /*
   * sg page iterator for DMA addresses
   *
   * This is the same as sg_page_iter however you can call
   * sg_page_iter_dma_address(@dma_iter) to get the page's DMA
   * address. sg_page_iter_page() cannot be called on this iterator.
   */
  struct sg_dma_page_iter {
  	struct sg_page_iter base;
  };
a321e91b6   Imre Deak   lib/scatterlist: ...
373
  bool __sg_page_iter_next(struct sg_page_iter *piter);
d901b2760   Jason Gunthorpe   lib/scatterlist: ...
374
  bool __sg_page_iter_dma_next(struct sg_dma_page_iter *dma_iter);
a321e91b6   Imre Deak   lib/scatterlist: ...
375
376
377
  void __sg_page_iter_start(struct sg_page_iter *piter,
  			  struct scatterlist *sglist, unsigned int nents,
  			  unsigned long pgoffset);
2db76d7c3   Imre Deak   lib/scatterlist: ...
378
379
380
381
382
383
384
385
386
387
388
389
  /**
   * sg_page_iter_page - get the current page held by the page iterator
   * @piter:	page iterator holding the page
   */
  static inline struct page *sg_page_iter_page(struct sg_page_iter *piter)
  {
  	return nth_page(sg_page(piter->sg), piter->sg_pgoffset);
  }
  
  /**
   * sg_page_iter_dma_address - get the dma address of the current page held by
   * the page iterator.
d901b2760   Jason Gunthorpe   lib/scatterlist: ...
390
   * @dma_iter:	page iterator holding the page
2db76d7c3   Imre Deak   lib/scatterlist: ...
391
   */
d901b2760   Jason Gunthorpe   lib/scatterlist: ...
392
393
  static inline dma_addr_t
  sg_page_iter_dma_address(struct sg_dma_page_iter *dma_iter)
2db76d7c3   Imre Deak   lib/scatterlist: ...
394
  {
d901b2760   Jason Gunthorpe   lib/scatterlist: ...
395
396
  	return sg_dma_address(dma_iter->base.sg) +
  	       (dma_iter->base.sg_pgoffset << PAGE_SHIFT);
2db76d7c3   Imre Deak   lib/scatterlist: ...
397
  }
a321e91b6   Imre Deak   lib/scatterlist: ...
398
399
400
401
402
403
  
  /**
   * for_each_sg_page - iterate over the pages of the given sg list
   * @sglist:	sglist to iterate over
   * @piter:	page iterator to hold current page, sg, sg_pgoffset
   * @nents:	maximum number of sg entries to iterate over
709d6d73c   Marek Szyprowski   scatterlist: add ...
404
   * @pgoffset:	starting page offset (in pages)
d901b2760   Jason Gunthorpe   lib/scatterlist: ...
405
406
   *
   * Callers may use sg_page_iter_page() to get each page pointer.
709d6d73c   Marek Szyprowski   scatterlist: add ...
407
   * In each loop it operates on PAGE_SIZE unit.
a321e91b6   Imre Deak   lib/scatterlist: ...
408
409
410
411
   */
  #define for_each_sg_page(sglist, piter, nents, pgoffset)		   \
  	for (__sg_page_iter_start((piter), (sglist), (nents), (pgoffset)); \
  	     __sg_page_iter_next(piter);)
137d3edb4   Tejun Heo   sg: reimplement s...
412

d901b2760   Jason Gunthorpe   lib/scatterlist: ...
413
414
415
  /**
   * for_each_sg_dma_page - iterate over the pages of the given sg list
   * @sglist:	sglist to iterate over
709d6d73c   Marek Szyprowski   scatterlist: add ...
416
   * @dma_iter:	DMA page iterator to hold current page
d901b2760   Jason Gunthorpe   lib/scatterlist: ...
417
418
   * @dma_nents:	maximum number of sg entries to iterate over, this is the value
   *              returned from dma_map_sg
709d6d73c   Marek Szyprowski   scatterlist: add ...
419
   * @pgoffset:	starting page offset (in pages)
d901b2760   Jason Gunthorpe   lib/scatterlist: ...
420
421
   *
   * Callers may use sg_page_iter_dma_address() to get each page's DMA address.
709d6d73c   Marek Szyprowski   scatterlist: add ...
422
   * In each loop it operates on PAGE_SIZE unit.
d901b2760   Jason Gunthorpe   lib/scatterlist: ...
423
424
425
426
427
   */
  #define for_each_sg_dma_page(sglist, dma_iter, dma_nents, pgoffset)            \
  	for (__sg_page_iter_start(&(dma_iter)->base, sglist, dma_nents,        \
  				  pgoffset);                                   \
  	     __sg_page_iter_dma_next(dma_iter);)
709d6d73c   Marek Szyprowski   scatterlist: add ...
428
429
430
431
432
433
434
435
436
437
438
  /**
   * for_each_sgtable_page - iterate over all pages in the sg_table object
   * @sgt:	sg_table object to iterate over
   * @piter:	page iterator to hold current page
   * @pgoffset:	starting page offset (in pages)
   *
   * Iterates over the all memory pages in the buffer described by
   * a scatterlist stored in the given sg_table object.
   * See also for_each_sg_page(). In each loop it operates on PAGE_SIZE unit.
   */
  #define for_each_sgtable_page(sgt, piter, pgoffset)	\
68d237056   Marek Szyprowski   scatterlist: prot...
439
  	for_each_sg_page((sgt)->sgl, piter, (sgt)->orig_nents, pgoffset)
709d6d73c   Marek Szyprowski   scatterlist: add ...
440
441
442
443
444
445
446
447
448
449
450
451
452
  
  /**
   * for_each_sgtable_dma_page - iterate over the DMA mapped sg_table object
   * @sgt:	sg_table object to iterate over
   * @dma_iter:	DMA page iterator to hold current page
   * @pgoffset:	starting page offset (in pages)
   *
   * Iterates over the all DMA mapped pages in the buffer described by
   * a scatterlist stored in the given sg_table object.
   * See also for_each_sg_dma_page(). In each loop it operates on PAGE_SIZE
   * unit.
   */
  #define for_each_sgtable_dma_page(sgt, dma_iter, pgoffset)	\
68d237056   Marek Szyprowski   scatterlist: prot...
453
  	for_each_sg_dma_page((sgt)->sgl, dma_iter, (sgt)->nents, pgoffset)
709d6d73c   Marek Szyprowski   scatterlist: add ...
454

137d3edb4   Tejun Heo   sg: reimplement s...
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
  /*
   * Mapping sg iterator
   *
   * Iterates over sg entries mapping page-by-page.  On each successful
   * iteration, @miter->page points to the mapped page and
   * @miter->length bytes of data can be accessed at @miter->addr.  As
   * long as an interation is enclosed between start and stop, the user
   * is free to choose control structure and when to stop.
   *
   * @miter->consumed is set to @miter->length on each iteration.  It
   * can be adjusted if the user can't consume all the bytes in one go.
   * Also, a stopped iteration can be resumed by calling next on it.
   * This is useful when iteration needs to release all resources and
   * continue later (e.g. at the next interrupt).
   */
  
  #define SG_MITER_ATOMIC		(1 << 0)	 /* use kmap_atomic */
6de7e356f   Sebastian Andrzej Siewior   lib/scatterlist: ...
472
473
  #define SG_MITER_TO_SG		(1 << 1)	/* flush back to phys on unmap */
  #define SG_MITER_FROM_SG	(1 << 2)	/* nop */
137d3edb4   Tejun Heo   sg: reimplement s...
474
475
476
477
478
479
480
  
  struct sg_mapping_iter {
  	/* the following three fields can be accessed directly */
  	struct page		*page;		/* currently mapped page */
  	void			*addr;		/* pointer to the mapped area */
  	size_t			length;		/* length of the mapped area */
  	size_t			consumed;	/* number of consumed bytes */
4225fc855   Imre Deak   lib/scatterlist: ...
481
  	struct sg_page_iter	piter;		/* page iterator */
137d3edb4   Tejun Heo   sg: reimplement s...
482
483
  
  	/* these are internal states, keep away */
4225fc855   Imre Deak   lib/scatterlist: ...
484
485
  	unsigned int		__offset;	/* offset within page */
  	unsigned int		__remaining;	/* remaining bytes on page */
137d3edb4   Tejun Heo   sg: reimplement s...
486
487
488
489
490
  	unsigned int		__flags;
  };
  
  void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
  		    unsigned int nents, unsigned int flags);
0d6077f8b   Ming Lei   lib/scatterlist: ...
491
  bool sg_miter_skip(struct sg_mapping_iter *miter, off_t offset);
137d3edb4   Tejun Heo   sg: reimplement s...
492
493
  bool sg_miter_next(struct sg_mapping_iter *miter);
  void sg_miter_stop(struct sg_mapping_iter *miter);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
494
  #endif /* _LINUX_SCATTERLIST_H */