1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _LINUX_SCATTERLIST_H
3*4882a593Smuzhiyun #define _LINUX_SCATTERLIST_H
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun #include <linux/string.h>
6*4882a593Smuzhiyun #include <linux/types.h>
7*4882a593Smuzhiyun #include <linux/bug.h>
8*4882a593Smuzhiyun #include <linux/mm.h>
9*4882a593Smuzhiyun #include <asm/io.h>
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun struct scatterlist {
12*4882a593Smuzhiyun unsigned long page_link;
13*4882a593Smuzhiyun unsigned int offset;
14*4882a593Smuzhiyun unsigned int length;
15*4882a593Smuzhiyun dma_addr_t dma_address;
16*4882a593Smuzhiyun #ifdef CONFIG_NEED_SG_DMA_LENGTH
17*4882a593Smuzhiyun unsigned int dma_length;
18*4882a593Smuzhiyun #endif
19*4882a593Smuzhiyun };
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun /*
22*4882a593Smuzhiyun * Since the above length field is an unsigned int, below we define the maximum
23*4882a593Smuzhiyun * length in bytes that can be stored in one scatterlist entry.
24*4882a593Smuzhiyun */
25*4882a593Smuzhiyun #define SCATTERLIST_MAX_SEGMENT (UINT_MAX & PAGE_MASK)
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun /*
28*4882a593Smuzhiyun * These macros should be used after a dma_map_sg call has been done
29*4882a593Smuzhiyun * to get bus addresses of each of the SG entries and their lengths.
30*4882a593Smuzhiyun * You should only work with the number of sg entries dma_map_sg
31*4882a593Smuzhiyun * returns, or alternatively stop on the first sg_dma_len(sg) which
32*4882a593Smuzhiyun * is 0.
33*4882a593Smuzhiyun */
34*4882a593Smuzhiyun #define sg_dma_address(sg) ((sg)->dma_address)
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #ifdef CONFIG_NEED_SG_DMA_LENGTH
37*4882a593Smuzhiyun #define sg_dma_len(sg) ((sg)->dma_length)
38*4882a593Smuzhiyun #else
39*4882a593Smuzhiyun #define sg_dma_len(sg) ((sg)->length)
40*4882a593Smuzhiyun #endif
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun struct sg_table {
43*4882a593Smuzhiyun struct scatterlist *sgl; /* the list */
44*4882a593Smuzhiyun unsigned int nents; /* number of mapped entries */
45*4882a593Smuzhiyun unsigned int orig_nents; /* original size of list */
46*4882a593Smuzhiyun };
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun /*
49*4882a593Smuzhiyun * Notes on SG table design.
50*4882a593Smuzhiyun *
51*4882a593Smuzhiyun * We use the unsigned long page_link field in the scatterlist struct to place
52*4882a593Smuzhiyun * the page pointer AND encode information about the sg table as well. The two
53*4882a593Smuzhiyun * lower bits are reserved for this information.
54*4882a593Smuzhiyun *
55*4882a593Smuzhiyun * If bit 0 is set, then the page_link contains a pointer to the next sg
56*4882a593Smuzhiyun * table list. Otherwise the next entry is at sg + 1.
57*4882a593Smuzhiyun *
58*4882a593Smuzhiyun * If bit 1 is set, then this sg entry is the last element in a list.
59*4882a593Smuzhiyun *
60*4882a593Smuzhiyun * See sg_next().
61*4882a593Smuzhiyun *
62*4882a593Smuzhiyun */
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun #define SG_CHAIN 0x01UL
65*4882a593Smuzhiyun #define SG_END 0x02UL
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun /*
68*4882a593Smuzhiyun * We overload the LSB of the page pointer to indicate whether it's
69*4882a593Smuzhiyun * a valid sg entry, or whether it points to the start of a new scatterlist.
70*4882a593Smuzhiyun * Those low bits are there for everyone! (thanks mason :-)
71*4882a593Smuzhiyun */
72*4882a593Smuzhiyun #define sg_is_chain(sg) ((sg)->page_link & SG_CHAIN)
73*4882a593Smuzhiyun #define sg_is_last(sg) ((sg)->page_link & SG_END)
74*4882a593Smuzhiyun #define sg_chain_ptr(sg) \
75*4882a593Smuzhiyun ((struct scatterlist *) ((sg)->page_link & ~(SG_CHAIN | SG_END)))
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /**
78*4882a593Smuzhiyun * sg_assign_page - Assign a given page to an SG entry
79*4882a593Smuzhiyun * @sg: SG entry
80*4882a593Smuzhiyun * @page: The page
81*4882a593Smuzhiyun *
82*4882a593Smuzhiyun * Description:
83*4882a593Smuzhiyun * Assign page to sg entry. Also see sg_set_page(), the most commonly used
84*4882a593Smuzhiyun * variant.
85*4882a593Smuzhiyun *
86*4882a593Smuzhiyun **/
sg_assign_page(struct scatterlist * sg,struct page * page)87*4882a593Smuzhiyun static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun unsigned long page_link = sg->page_link & (SG_CHAIN | SG_END);
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /*
92*4882a593Smuzhiyun * In order for the low bit stealing approach to work, pages
93*4882a593Smuzhiyun * must be aligned at a 32-bit boundary as a minimum.
94*4882a593Smuzhiyun */
95*4882a593Smuzhiyun BUG_ON((unsigned long) page & (SG_CHAIN | SG_END));
96*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_SG
97*4882a593Smuzhiyun BUG_ON(sg_is_chain(sg));
98*4882a593Smuzhiyun #endif
99*4882a593Smuzhiyun sg->page_link = page_link | (unsigned long) page;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun /**
103*4882a593Smuzhiyun * sg_set_page - Set sg entry to point at given page
104*4882a593Smuzhiyun * @sg: SG entry
105*4882a593Smuzhiyun * @page: The page
106*4882a593Smuzhiyun * @len: Length of data
107*4882a593Smuzhiyun * @offset: Offset into page
108*4882a593Smuzhiyun *
109*4882a593Smuzhiyun * Description:
110*4882a593Smuzhiyun * Use this function to set an sg entry pointing at a page, never assign
111*4882a593Smuzhiyun * the page directly. We encode sg table information in the lower bits
112*4882a593Smuzhiyun * of the page pointer. See sg_page() for looking up the page belonging
113*4882a593Smuzhiyun * to an sg entry.
114*4882a593Smuzhiyun *
115*4882a593Smuzhiyun **/
sg_set_page(struct scatterlist * sg,struct page * page,unsigned int len,unsigned int offset)116*4882a593Smuzhiyun static inline void sg_set_page(struct scatterlist *sg, struct page *page,
117*4882a593Smuzhiyun unsigned int len, unsigned int offset)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun sg_assign_page(sg, page);
120*4882a593Smuzhiyun sg->offset = offset;
121*4882a593Smuzhiyun sg->length = len;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun
sg_page(struct scatterlist * sg)124*4882a593Smuzhiyun static inline struct page *sg_page(struct scatterlist *sg)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_SG
127*4882a593Smuzhiyun BUG_ON(sg_is_chain(sg));
128*4882a593Smuzhiyun #endif
129*4882a593Smuzhiyun return (struct page *)((sg)->page_link & ~(SG_CHAIN | SG_END));
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun /**
133*4882a593Smuzhiyun * sg_set_buf - Set sg entry to point at given data
134*4882a593Smuzhiyun * @sg: SG entry
135*4882a593Smuzhiyun * @buf: Data
136*4882a593Smuzhiyun * @buflen: Data length
137*4882a593Smuzhiyun *
138*4882a593Smuzhiyun **/
sg_set_buf(struct scatterlist * sg,const void * buf,unsigned int buflen)139*4882a593Smuzhiyun static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
140*4882a593Smuzhiyun unsigned int buflen)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_SG
143*4882a593Smuzhiyun BUG_ON(!virt_addr_valid(buf));
144*4882a593Smuzhiyun #endif
145*4882a593Smuzhiyun sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun /*
149*4882a593Smuzhiyun * Loop over each sg element, following the pointer to a new list if necessary
150*4882a593Smuzhiyun */
151*4882a593Smuzhiyun #define for_each_sg(sglist, sg, nr, __i) \
152*4882a593Smuzhiyun for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun /*
155*4882a593Smuzhiyun * Loop over each sg element in the given sg_table object.
156*4882a593Smuzhiyun */
157*4882a593Smuzhiyun #define for_each_sgtable_sg(sgt, sg, i) \
158*4882a593Smuzhiyun for_each_sg((sgt)->sgl, sg, (sgt)->orig_nents, i)
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun /*
161*4882a593Smuzhiyun * Loop over each sg element in the given *DMA mapped* sg_table object.
162*4882a593Smuzhiyun * Please use sg_dma_address(sg) and sg_dma_len(sg) to extract DMA addresses
163*4882a593Smuzhiyun * of the each element.
164*4882a593Smuzhiyun */
165*4882a593Smuzhiyun #define for_each_sgtable_dma_sg(sgt, sg, i) \
166*4882a593Smuzhiyun for_each_sg((sgt)->sgl, sg, (sgt)->nents, i)
167*4882a593Smuzhiyun
__sg_chain(struct scatterlist * chain_sg,struct scatterlist * sgl)168*4882a593Smuzhiyun static inline void __sg_chain(struct scatterlist *chain_sg,
169*4882a593Smuzhiyun struct scatterlist *sgl)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun /*
172*4882a593Smuzhiyun * offset and length are unused for chain entry. Clear them.
173*4882a593Smuzhiyun */
174*4882a593Smuzhiyun chain_sg->offset = 0;
175*4882a593Smuzhiyun chain_sg->length = 0;
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun /*
178*4882a593Smuzhiyun * Set lowest bit to indicate a link pointer, and make sure to clear
179*4882a593Smuzhiyun * the termination bit if it happens to be set.
180*4882a593Smuzhiyun */
181*4882a593Smuzhiyun chain_sg->page_link = ((unsigned long) sgl | SG_CHAIN) & ~SG_END;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun /**
185*4882a593Smuzhiyun * sg_chain - Chain two sglists together
186*4882a593Smuzhiyun * @prv: First scatterlist
187*4882a593Smuzhiyun * @prv_nents: Number of entries in prv
188*4882a593Smuzhiyun * @sgl: Second scatterlist
189*4882a593Smuzhiyun *
190*4882a593Smuzhiyun * Description:
191*4882a593Smuzhiyun * Links @prv@ and @sgl@ together, to form a longer scatterlist.
192*4882a593Smuzhiyun *
193*4882a593Smuzhiyun **/
sg_chain(struct scatterlist * prv,unsigned int prv_nents,struct scatterlist * sgl)194*4882a593Smuzhiyun static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents,
195*4882a593Smuzhiyun struct scatterlist *sgl)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun __sg_chain(&prv[prv_nents - 1], sgl);
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun /**
201*4882a593Smuzhiyun * sg_mark_end - Mark the end of the scatterlist
202*4882a593Smuzhiyun * @sg: SG entryScatterlist
203*4882a593Smuzhiyun *
204*4882a593Smuzhiyun * Description:
205*4882a593Smuzhiyun * Marks the passed in sg entry as the termination point for the sg
206*4882a593Smuzhiyun * table. A call to sg_next() on this entry will return NULL.
207*4882a593Smuzhiyun *
208*4882a593Smuzhiyun **/
sg_mark_end(struct scatterlist * sg)209*4882a593Smuzhiyun static inline void sg_mark_end(struct scatterlist *sg)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun /*
212*4882a593Smuzhiyun * Set termination bit, clear potential chain bit
213*4882a593Smuzhiyun */
214*4882a593Smuzhiyun sg->page_link |= SG_END;
215*4882a593Smuzhiyun sg->page_link &= ~SG_CHAIN;
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun /**
219*4882a593Smuzhiyun * sg_unmark_end - Undo setting the end of the scatterlist
220*4882a593Smuzhiyun * @sg: SG entryScatterlist
221*4882a593Smuzhiyun *
222*4882a593Smuzhiyun * Description:
223*4882a593Smuzhiyun * Removes the termination marker from the given entry of the scatterlist.
224*4882a593Smuzhiyun *
225*4882a593Smuzhiyun **/
sg_unmark_end(struct scatterlist * sg)226*4882a593Smuzhiyun static inline void sg_unmark_end(struct scatterlist *sg)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun sg->page_link &= ~SG_END;
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun /**
232*4882a593Smuzhiyun * sg_phys - Return physical address of an sg entry
233*4882a593Smuzhiyun * @sg: SG entry
234*4882a593Smuzhiyun *
235*4882a593Smuzhiyun * Description:
236*4882a593Smuzhiyun * This calls page_to_phys() on the page in this sg entry, and adds the
237*4882a593Smuzhiyun * sg offset. The caller must know that it is legal to call page_to_phys()
238*4882a593Smuzhiyun * on the sg page.
239*4882a593Smuzhiyun *
240*4882a593Smuzhiyun **/
sg_phys(struct scatterlist * sg)241*4882a593Smuzhiyun static inline dma_addr_t sg_phys(struct scatterlist *sg)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun return page_to_phys(sg_page(sg)) + sg->offset;
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun /**
247*4882a593Smuzhiyun * sg_virt - Return virtual address of an sg entry
248*4882a593Smuzhiyun * @sg: SG entry
249*4882a593Smuzhiyun *
250*4882a593Smuzhiyun * Description:
251*4882a593Smuzhiyun * This calls page_address() on the page in this sg entry, and adds the
252*4882a593Smuzhiyun * sg offset. The caller must know that the sg page has a valid virtual
253*4882a593Smuzhiyun * mapping.
254*4882a593Smuzhiyun *
255*4882a593Smuzhiyun **/
sg_virt(struct scatterlist * sg)256*4882a593Smuzhiyun static inline void *sg_virt(struct scatterlist *sg)
257*4882a593Smuzhiyun {
258*4882a593Smuzhiyun return page_address(sg_page(sg)) + sg->offset;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun /**
262*4882a593Smuzhiyun * sg_init_marker - Initialize markers in sg table
263*4882a593Smuzhiyun * @sgl: The SG table
264*4882a593Smuzhiyun * @nents: Number of entries in table
265*4882a593Smuzhiyun *
266*4882a593Smuzhiyun **/
sg_init_marker(struct scatterlist * sgl,unsigned int nents)267*4882a593Smuzhiyun static inline void sg_init_marker(struct scatterlist *sgl,
268*4882a593Smuzhiyun unsigned int nents)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun sg_mark_end(&sgl[nents - 1]);
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun int sg_nents(struct scatterlist *sg);
274*4882a593Smuzhiyun int sg_nents_for_len(struct scatterlist *sg, u64 len);
275*4882a593Smuzhiyun struct scatterlist *sg_next(struct scatterlist *);
276*4882a593Smuzhiyun struct scatterlist *sg_last(struct scatterlist *s, unsigned int);
277*4882a593Smuzhiyun void sg_init_table(struct scatterlist *, unsigned int);
278*4882a593Smuzhiyun void sg_init_one(struct scatterlist *, const void *, unsigned int);
279*4882a593Smuzhiyun int sg_split(struct scatterlist *in, const int in_mapped_nents,
280*4882a593Smuzhiyun const off_t skip, const int nb_splits,
281*4882a593Smuzhiyun const size_t *split_sizes,
282*4882a593Smuzhiyun struct scatterlist **out, int *out_mapped_nents,
283*4882a593Smuzhiyun gfp_t gfp_mask);
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun typedef struct scatterlist *(sg_alloc_fn)(unsigned int, gfp_t);
286*4882a593Smuzhiyun typedef void (sg_free_fn)(struct scatterlist *, unsigned int);
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun void __sg_free_table(struct sg_table *, unsigned int, unsigned int,
289*4882a593Smuzhiyun sg_free_fn *);
290*4882a593Smuzhiyun void sg_free_table(struct sg_table *);
291*4882a593Smuzhiyun int __sg_alloc_table(struct sg_table *, unsigned int, unsigned int,
292*4882a593Smuzhiyun struct scatterlist *, unsigned int, gfp_t, sg_alloc_fn *);
293*4882a593Smuzhiyun int sg_alloc_table(struct sg_table *, unsigned int, gfp_t);
294*4882a593Smuzhiyun struct scatterlist *__sg_alloc_table_from_pages(struct sg_table *sgt,
295*4882a593Smuzhiyun struct page **pages, unsigned int n_pages, unsigned int offset,
296*4882a593Smuzhiyun unsigned long size, unsigned int max_segment,
297*4882a593Smuzhiyun struct scatterlist *prv, unsigned int left_pages,
298*4882a593Smuzhiyun gfp_t gfp_mask);
299*4882a593Smuzhiyun int sg_alloc_table_from_pages(struct sg_table *sgt, struct page **pages,
300*4882a593Smuzhiyun unsigned int n_pages, unsigned int offset,
301*4882a593Smuzhiyun unsigned long size, gfp_t gfp_mask);
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun #ifdef CONFIG_SGL_ALLOC
304*4882a593Smuzhiyun struct scatterlist *sgl_alloc_order(unsigned long long length,
305*4882a593Smuzhiyun unsigned int order, bool chainable,
306*4882a593Smuzhiyun gfp_t gfp, unsigned int *nent_p);
307*4882a593Smuzhiyun struct scatterlist *sgl_alloc(unsigned long long length, gfp_t gfp,
308*4882a593Smuzhiyun unsigned int *nent_p);
309*4882a593Smuzhiyun void sgl_free_n_order(struct scatterlist *sgl, int nents, int order);
310*4882a593Smuzhiyun void sgl_free_order(struct scatterlist *sgl, int order);
311*4882a593Smuzhiyun void sgl_free(struct scatterlist *sgl);
312*4882a593Smuzhiyun #endif /* CONFIG_SGL_ALLOC */
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf,
315*4882a593Smuzhiyun size_t buflen, off_t skip, bool to_buffer);
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
318*4882a593Smuzhiyun const void *buf, size_t buflen);
319*4882a593Smuzhiyun size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
320*4882a593Smuzhiyun void *buf, size_t buflen);
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun size_t sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents,
323*4882a593Smuzhiyun const void *buf, size_t buflen, off_t skip);
324*4882a593Smuzhiyun size_t sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents,
325*4882a593Smuzhiyun void *buf, size_t buflen, off_t skip);
326*4882a593Smuzhiyun size_t sg_zero_buffer(struct scatterlist *sgl, unsigned int nents,
327*4882a593Smuzhiyun size_t buflen, off_t skip);
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun /*
330*4882a593Smuzhiyun * Maximum number of entries that will be allocated in one piece, if
331*4882a593Smuzhiyun * a list larger than this is required then chaining will be utilized.
332*4882a593Smuzhiyun */
333*4882a593Smuzhiyun #define SG_MAX_SINGLE_ALLOC (PAGE_SIZE / sizeof(struct scatterlist))
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun /*
336*4882a593Smuzhiyun * The maximum number of SG segments that we will put inside a
337*4882a593Smuzhiyun * scatterlist (unless chaining is used). Should ideally fit inside a
338*4882a593Smuzhiyun * single page, to avoid a higher order allocation. We could define this
339*4882a593Smuzhiyun * to SG_MAX_SINGLE_ALLOC to pack correctly at the highest order. The
340*4882a593Smuzhiyun * minimum value is 32
341*4882a593Smuzhiyun */
342*4882a593Smuzhiyun #define SG_CHUNK_SIZE 128
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun /*
345*4882a593Smuzhiyun * Like SG_CHUNK_SIZE, but for archs that have sg chaining. This limit
346*4882a593Smuzhiyun * is totally arbitrary, a setting of 2048 will get you at least 8mb ios.
347*4882a593Smuzhiyun */
348*4882a593Smuzhiyun #ifdef CONFIG_ARCH_NO_SG_CHAIN
349*4882a593Smuzhiyun #define SG_MAX_SEGMENTS SG_CHUNK_SIZE
350*4882a593Smuzhiyun #else
351*4882a593Smuzhiyun #define SG_MAX_SEGMENTS 2048
352*4882a593Smuzhiyun #endif
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun #ifdef CONFIG_SG_POOL
355*4882a593Smuzhiyun void sg_free_table_chained(struct sg_table *table,
356*4882a593Smuzhiyun unsigned nents_first_chunk);
357*4882a593Smuzhiyun int sg_alloc_table_chained(struct sg_table *table, int nents,
358*4882a593Smuzhiyun struct scatterlist *first_chunk,
359*4882a593Smuzhiyun unsigned nents_first_chunk);
360*4882a593Smuzhiyun #endif
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun /*
363*4882a593Smuzhiyun * sg page iterator
364*4882a593Smuzhiyun *
365*4882a593Smuzhiyun * Iterates over sg entries page-by-page. On each successful iteration, you
366*4882a593Smuzhiyun * can call sg_page_iter_page(@piter) to get the current page.
367*4882a593Smuzhiyun * @piter->sg will point to the sg holding this page and @piter->sg_pgoffset to
368*4882a593Smuzhiyun * the page's page offset within the sg. The iteration will stop either when a
369*4882a593Smuzhiyun * maximum number of sg entries was reached or a terminating sg
370*4882a593Smuzhiyun * (sg_last(sg) == true) was reached.
371*4882a593Smuzhiyun */
372*4882a593Smuzhiyun struct sg_page_iter {
373*4882a593Smuzhiyun struct scatterlist *sg; /* sg holding the page */
374*4882a593Smuzhiyun unsigned int sg_pgoffset; /* page offset within the sg */
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun /* these are internal states, keep away */
377*4882a593Smuzhiyun unsigned int __nents; /* remaining sg entries */
378*4882a593Smuzhiyun int __pg_advance; /* nr pages to advance at the
379*4882a593Smuzhiyun * next step */
380*4882a593Smuzhiyun };
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun /*
383*4882a593Smuzhiyun * sg page iterator for DMA addresses
384*4882a593Smuzhiyun *
385*4882a593Smuzhiyun * This is the same as sg_page_iter however you can call
386*4882a593Smuzhiyun * sg_page_iter_dma_address(@dma_iter) to get the page's DMA
387*4882a593Smuzhiyun * address. sg_page_iter_page() cannot be called on this iterator.
388*4882a593Smuzhiyun */
389*4882a593Smuzhiyun struct sg_dma_page_iter {
390*4882a593Smuzhiyun struct sg_page_iter base;
391*4882a593Smuzhiyun };
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun bool __sg_page_iter_next(struct sg_page_iter *piter);
394*4882a593Smuzhiyun bool __sg_page_iter_dma_next(struct sg_dma_page_iter *dma_iter);
395*4882a593Smuzhiyun void __sg_page_iter_start(struct sg_page_iter *piter,
396*4882a593Smuzhiyun struct scatterlist *sglist, unsigned int nents,
397*4882a593Smuzhiyun unsigned long pgoffset);
398*4882a593Smuzhiyun /**
399*4882a593Smuzhiyun * sg_page_iter_page - get the current page held by the page iterator
400*4882a593Smuzhiyun * @piter: page iterator holding the page
401*4882a593Smuzhiyun */
sg_page_iter_page(struct sg_page_iter * piter)402*4882a593Smuzhiyun static inline struct page *sg_page_iter_page(struct sg_page_iter *piter)
403*4882a593Smuzhiyun {
404*4882a593Smuzhiyun return nth_page(sg_page(piter->sg), piter->sg_pgoffset);
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun /**
408*4882a593Smuzhiyun * sg_page_iter_dma_address - get the dma address of the current page held by
409*4882a593Smuzhiyun * the page iterator.
410*4882a593Smuzhiyun * @dma_iter: page iterator holding the page
411*4882a593Smuzhiyun */
412*4882a593Smuzhiyun static inline dma_addr_t
sg_page_iter_dma_address(struct sg_dma_page_iter * dma_iter)413*4882a593Smuzhiyun sg_page_iter_dma_address(struct sg_dma_page_iter *dma_iter)
414*4882a593Smuzhiyun {
415*4882a593Smuzhiyun return sg_dma_address(dma_iter->base.sg) +
416*4882a593Smuzhiyun (dma_iter->base.sg_pgoffset << PAGE_SHIFT);
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun /**
420*4882a593Smuzhiyun * for_each_sg_page - iterate over the pages of the given sg list
421*4882a593Smuzhiyun * @sglist: sglist to iterate over
422*4882a593Smuzhiyun * @piter: page iterator to hold current page, sg, sg_pgoffset
423*4882a593Smuzhiyun * @nents: maximum number of sg entries to iterate over
424*4882a593Smuzhiyun * @pgoffset: starting page offset (in pages)
425*4882a593Smuzhiyun *
426*4882a593Smuzhiyun * Callers may use sg_page_iter_page() to get each page pointer.
427*4882a593Smuzhiyun * In each loop it operates on PAGE_SIZE unit.
428*4882a593Smuzhiyun */
429*4882a593Smuzhiyun #define for_each_sg_page(sglist, piter, nents, pgoffset) \
430*4882a593Smuzhiyun for (__sg_page_iter_start((piter), (sglist), (nents), (pgoffset)); \
431*4882a593Smuzhiyun __sg_page_iter_next(piter);)
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun /**
434*4882a593Smuzhiyun * for_each_sg_dma_page - iterate over the pages of the given sg list
435*4882a593Smuzhiyun * @sglist: sglist to iterate over
436*4882a593Smuzhiyun * @dma_iter: DMA page iterator to hold current page
437*4882a593Smuzhiyun * @dma_nents: maximum number of sg entries to iterate over, this is the value
438*4882a593Smuzhiyun * returned from dma_map_sg
439*4882a593Smuzhiyun * @pgoffset: starting page offset (in pages)
440*4882a593Smuzhiyun *
441*4882a593Smuzhiyun * Callers may use sg_page_iter_dma_address() to get each page's DMA address.
442*4882a593Smuzhiyun * In each loop it operates on PAGE_SIZE unit.
443*4882a593Smuzhiyun */
444*4882a593Smuzhiyun #define for_each_sg_dma_page(sglist, dma_iter, dma_nents, pgoffset) \
445*4882a593Smuzhiyun for (__sg_page_iter_start(&(dma_iter)->base, sglist, dma_nents, \
446*4882a593Smuzhiyun pgoffset); \
447*4882a593Smuzhiyun __sg_page_iter_dma_next(dma_iter);)
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun /**
450*4882a593Smuzhiyun * for_each_sgtable_page - iterate over all pages in the sg_table object
451*4882a593Smuzhiyun * @sgt: sg_table object to iterate over
452*4882a593Smuzhiyun * @piter: page iterator to hold current page
453*4882a593Smuzhiyun * @pgoffset: starting page offset (in pages)
454*4882a593Smuzhiyun *
455*4882a593Smuzhiyun * Iterates over the all memory pages in the buffer described by
456*4882a593Smuzhiyun * a scatterlist stored in the given sg_table object.
457*4882a593Smuzhiyun * See also for_each_sg_page(). In each loop it operates on PAGE_SIZE unit.
458*4882a593Smuzhiyun */
459*4882a593Smuzhiyun #define for_each_sgtable_page(sgt, piter, pgoffset) \
460*4882a593Smuzhiyun for_each_sg_page((sgt)->sgl, piter, (sgt)->orig_nents, pgoffset)
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun /**
463*4882a593Smuzhiyun * for_each_sgtable_dma_page - iterate over the DMA mapped sg_table object
464*4882a593Smuzhiyun * @sgt: sg_table object to iterate over
465*4882a593Smuzhiyun * @dma_iter: DMA page iterator to hold current page
466*4882a593Smuzhiyun * @pgoffset: starting page offset (in pages)
467*4882a593Smuzhiyun *
468*4882a593Smuzhiyun * Iterates over the all DMA mapped pages in the buffer described by
469*4882a593Smuzhiyun * a scatterlist stored in the given sg_table object.
470*4882a593Smuzhiyun * See also for_each_sg_dma_page(). In each loop it operates on PAGE_SIZE
471*4882a593Smuzhiyun * unit.
472*4882a593Smuzhiyun */
473*4882a593Smuzhiyun #define for_each_sgtable_dma_page(sgt, dma_iter, pgoffset) \
474*4882a593Smuzhiyun for_each_sg_dma_page((sgt)->sgl, dma_iter, (sgt)->nents, pgoffset)
475*4882a593Smuzhiyun
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun /*
478*4882a593Smuzhiyun * Mapping sg iterator
479*4882a593Smuzhiyun *
480*4882a593Smuzhiyun * Iterates over sg entries mapping page-by-page. On each successful
481*4882a593Smuzhiyun * iteration, @miter->page points to the mapped page and
482*4882a593Smuzhiyun * @miter->length bytes of data can be accessed at @miter->addr. As
483*4882a593Smuzhiyun * long as an interation is enclosed between start and stop, the user
484*4882a593Smuzhiyun * is free to choose control structure and when to stop.
485*4882a593Smuzhiyun *
486*4882a593Smuzhiyun * @miter->consumed is set to @miter->length on each iteration. It
487*4882a593Smuzhiyun * can be adjusted if the user can't consume all the bytes in one go.
488*4882a593Smuzhiyun * Also, a stopped iteration can be resumed by calling next on it.
489*4882a593Smuzhiyun * This is useful when iteration needs to release all resources and
490*4882a593Smuzhiyun * continue later (e.g. at the next interrupt).
491*4882a593Smuzhiyun */
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun #define SG_MITER_ATOMIC (1 << 0) /* use kmap_atomic */
494*4882a593Smuzhiyun #define SG_MITER_TO_SG (1 << 1) /* flush back to phys on unmap */
495*4882a593Smuzhiyun #define SG_MITER_FROM_SG (1 << 2) /* nop */
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun struct sg_mapping_iter {
498*4882a593Smuzhiyun /* the following three fields can be accessed directly */
499*4882a593Smuzhiyun struct page *page; /* currently mapped page */
500*4882a593Smuzhiyun void *addr; /* pointer to the mapped area */
501*4882a593Smuzhiyun size_t length; /* length of the mapped area */
502*4882a593Smuzhiyun size_t consumed; /* number of consumed bytes */
503*4882a593Smuzhiyun struct sg_page_iter piter; /* page iterator */
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun /* these are internal states, keep away */
506*4882a593Smuzhiyun unsigned int __offset; /* offset within page */
507*4882a593Smuzhiyun unsigned int __remaining; /* remaining bytes on page */
508*4882a593Smuzhiyun unsigned int __flags;
509*4882a593Smuzhiyun };
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
512*4882a593Smuzhiyun unsigned int nents, unsigned int flags);
513*4882a593Smuzhiyun bool sg_miter_skip(struct sg_mapping_iter *miter, off_t offset);
514*4882a593Smuzhiyun bool sg_miter_next(struct sg_mapping_iter *miter);
515*4882a593Smuzhiyun void sg_miter_stop(struct sg_mapping_iter *miter);
516*4882a593Smuzhiyun
517*4882a593Smuzhiyun #endif /* _LINUX_SCATTERLIST_H */
518