blob: 6dd2ddbc62301f545888e4983da41ab5ba33b07c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_SCATTERLIST_H
2#define _LINUX_SCATTERLIST_H
3
Paul Gortmaker187f1882011-11-23 20:12:59 -05004#include <linux/string.h>
Christoph Hellwig84be4562015-05-01 12:46:15 +02005#include <linux/types.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -05006#include <linux/bug.h>
7#include <linux/mm.h>
Jens Axboe18dabf42007-10-22 19:57:20 +02008#include <asm/io.h>
9
Christoph Hellwig84be4562015-05-01 12:46:15 +020010struct scatterlist {
11#ifdef CONFIG_DEBUG_SG
12 unsigned long sg_magic;
13#endif
14 unsigned long page_link;
15 unsigned int offset;
16 unsigned int length;
17 dma_addr_t dma_address;
18#ifdef CONFIG_NEED_SG_DMA_LENGTH
19 unsigned int dma_length;
20#endif
21};
22
23/*
Tvrtko Ursulinc1259062017-08-03 10:13:12 +010024 * Since the above length field is an unsigned int, below we define the maximum
25 * length in bytes that can be stored in one scatterlist entry.
26 */
27#define SCATTERLIST_MAX_SEGMENT (UINT_MAX & PAGE_MASK)
28
29/*
Christoph Hellwig84be4562015-05-01 12:46:15 +020030 * These macros should be used after a dma_map_sg call has been done
31 * to get bus addresses of each of the SG entries and their lengths.
32 * You should only work with the number of sg entries dma_map_sg
33 * returns, or alternatively stop on the first sg_dma_len(sg) which
34 * is 0.
35 */
36#define sg_dma_address(sg) ((sg)->dma_address)
37
38#ifdef CONFIG_NEED_SG_DMA_LENGTH
39#define sg_dma_len(sg) ((sg)->dma_length)
40#else
41#define sg_dma_len(sg) ((sg)->length)
42#endif
43
Jens Axboe0db92992007-11-30 09:16:50 +010044struct sg_table {
45 struct scatterlist *sgl; /* the list */
46 unsigned int nents; /* number of mapped entries */
47 unsigned int orig_nents; /* original size of list */
48};
49
Jens Axboe18dabf42007-10-22 19:57:20 +020050/*
51 * Notes on SG table design.
52 *
Christoph Hellwig84be4562015-05-01 12:46:15 +020053 * We use the unsigned long page_link field in the scatterlist struct to place
54 * the page pointer AND encode information about the sg table as well. The two
55 * lower bits are reserved for this information.
Jens Axboe18dabf42007-10-22 19:57:20 +020056 *
57 * If bit 0 is set, then the page_link contains a pointer to the next sg
58 * table list. Otherwise the next entry is at sg + 1.
59 *
60 * If bit 1 is set, then this sg entry is the last element in a list.
61 *
62 * See sg_next().
63 *
64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Jens Axboed6ec0842007-10-22 20:01:06 +020066#define SG_MAGIC 0x87654321
67
Tejun Heo645a8d92007-11-27 09:30:39 +010068/*
69 * We overload the LSB of the page pointer to indicate whether it's
70 * a valid sg entry, or whether it points to the start of a new scatterlist.
71 * Those low bits are there for everyone! (thanks mason :-)
72 */
73#define sg_is_chain(sg) ((sg)->page_link & 0x01)
74#define sg_is_last(sg) ((sg)->page_link & 0x02)
75#define sg_chain_ptr(sg) \
76 ((struct scatterlist *) ((sg)->page_link & ~0x03))
77
Jens Axboe82f66fb2007-10-22 17:07:37 +020078/**
Jens Axboe642f149032007-10-24 11:20:47 +020079 * sg_assign_page - Assign a given page to an SG entry
80 * @sg: SG entry
81 * @page: The page
Jens Axboe82f66fb2007-10-22 17:07:37 +020082 *
83 * Description:
Jens Axboe642f149032007-10-24 11:20:47 +020084 * Assign page to sg entry. Also see sg_set_page(), the most commonly used
85 * variant.
Jens Axboe82f66fb2007-10-22 17:07:37 +020086 *
87 **/
Jens Axboe642f149032007-10-24 11:20:47 +020088static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
Jens Axboe82f66fb2007-10-22 17:07:37 +020089{
Jens Axboe18dabf42007-10-22 19:57:20 +020090 unsigned long page_link = sg->page_link & 0x3;
91
Jens Axboede261032007-10-23 20:35:58 +020092 /*
93 * In order for the low bit stealing approach to work, pages
94 * must be aligned at a 32-bit boundary as a minimum.
95 */
96 BUG_ON((unsigned long) page & 0x03);
Jens Axboed6ec0842007-10-22 20:01:06 +020097#ifdef CONFIG_DEBUG_SG
98 BUG_ON(sg->sg_magic != SG_MAGIC);
Tejun Heo645a8d92007-11-27 09:30:39 +010099 BUG_ON(sg_is_chain(sg));
Jens Axboed6ec0842007-10-22 20:01:06 +0200100#endif
Jens Axboe18dabf42007-10-22 19:57:20 +0200101 sg->page_link = page_link | (unsigned long) page;
Jens Axboe82f66fb2007-10-22 17:07:37 +0200102}
103
Jens Axboe642f149032007-10-24 11:20:47 +0200104/**
105 * sg_set_page - Set sg entry to point at given page
106 * @sg: SG entry
107 * @page: The page
108 * @len: Length of data
109 * @offset: Offset into page
110 *
111 * Description:
112 * Use this function to set an sg entry pointing at a page, never assign
113 * the page directly. We encode sg table information in the lower bits
114 * of the page pointer. See sg_page() for looking up the page belonging
115 * to an sg entry.
116 *
117 **/
118static inline void sg_set_page(struct scatterlist *sg, struct page *page,
119 unsigned int len, unsigned int offset)
120{
121 sg_assign_page(sg, page);
122 sg->offset = offset;
123 sg->length = len;
124}
125
Tejun Heo645a8d92007-11-27 09:30:39 +0100126static inline struct page *sg_page(struct scatterlist *sg)
127{
128#ifdef CONFIG_DEBUG_SG
129 BUG_ON(sg->sg_magic != SG_MAGIC);
130 BUG_ON(sg_is_chain(sg));
131#endif
132 return (struct page *)((sg)->page_link & ~0x3);
133}
Jens Axboe82f66fb2007-10-22 17:07:37 +0200134
Jens Axboe18dabf42007-10-22 19:57:20 +0200135/**
136 * sg_set_buf - Set sg entry to point at given data
137 * @sg: SG entry
138 * @buf: Data
139 * @buflen: Data length
140 *
141 **/
Herbert Xu03fd9ce2006-08-14 23:11:53 +1000142static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
Herbert Xud32311f2005-09-17 14:41:40 +1000143 unsigned int buflen)
144{
Rusty Russellac4e97a2013-05-30 09:19:35 +0200145#ifdef CONFIG_DEBUG_SG
146 BUG_ON(!virt_addr_valid(buf));
147#endif
Jens Axboe642f149032007-10-24 11:20:47 +0200148 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Jens Axboe96b418c2007-05-09 09:02:57 +0200151/*
152 * Loop over each sg element, following the pointer to a new list if necessary
153 */
154#define for_each_sg(sglist, sg, nr, __i) \
155 for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
156
Jens Axboe70eb8042007-07-16 21:17:16 +0200157/**
Jens Axboe70eb8042007-07-16 21:17:16 +0200158 * sg_chain - Chain two sglists together
159 * @prv: First scatterlist
160 * @prv_nents: Number of entries in prv
161 * @sgl: Second scatterlist
162 *
Jens Axboe18dabf42007-10-22 19:57:20 +0200163 * Description:
164 * Links @prv@ and @sgl@ together, to form a longer scatterlist.
Jens Axboe70eb8042007-07-16 21:17:16 +0200165 *
Jens Axboe18dabf42007-10-22 19:57:20 +0200166 **/
Jens Axboe70eb8042007-07-16 21:17:16 +0200167static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents,
168 struct scatterlist *sgl)
169{
Tejun Heo645a8d92007-11-27 09:30:39 +0100170 /*
171 * offset and length are unused for chain entry. Clear them.
172 */
Rusty Russellb801a1e2008-01-11 10:12:55 +0100173 prv[prv_nents - 1].offset = 0;
174 prv[prv_nents - 1].length = 0;
Tejun Heo645a8d92007-11-27 09:30:39 +0100175
Jens Axboe73fd5462007-10-26 09:32:16 +0200176 /*
177 * Set lowest bit to indicate a link pointer, and make sure to clear
178 * the termination bit if it happens to be set.
179 */
180 prv[prv_nents - 1].page_link = ((unsigned long) sgl | 0x01) & ~0x02;
Jens Axboe70eb8042007-07-16 21:17:16 +0200181}
182
Jens Axboe82f66fb2007-10-22 17:07:37 +0200183/**
184 * sg_mark_end - Mark the end of the scatterlist
Jens Axboec46f2332007-10-31 12:06:37 +0100185 * @sg: SG entryScatterlist
Jens Axboe82f66fb2007-10-22 17:07:37 +0200186 *
187 * Description:
Jens Axboec46f2332007-10-31 12:06:37 +0100188 * Marks the passed in sg entry as the termination point for the sg
189 * table. A call to sg_next() on this entry will return NULL.
Jens Axboe82f66fb2007-10-22 17:07:37 +0200190 *
191 **/
Jens Axboec46f2332007-10-31 12:06:37 +0100192static inline void sg_mark_end(struct scatterlist *sg)
Jens Axboe82f66fb2007-10-22 17:07:37 +0200193{
Jens Axboec46f2332007-10-31 12:06:37 +0100194#ifdef CONFIG_DEBUG_SG
195 BUG_ON(sg->sg_magic != SG_MAGIC);
196#endif
197 /*
198 * Set termination bit, clear potential chain bit
199 */
Jens Axboe18dabf42007-10-22 19:57:20 +0200200 sg->page_link |= 0x02;
Jens Axboec46f2332007-10-31 12:06:37 +0100201 sg->page_link &= ~0x01;
Jens Axboe82f66fb2007-10-22 17:07:37 +0200202}
203
Jens Axboe82f66fb2007-10-22 17:07:37 +0200204/**
Paolo Bonzinic8164d82013-03-20 15:37:08 +1030205 * sg_unmark_end - Undo setting the end of the scatterlist
206 * @sg: SG entryScatterlist
207 *
208 * Description:
209 * Removes the termination marker from the given entry of the scatterlist.
210 *
211 **/
212static inline void sg_unmark_end(struct scatterlist *sg)
213{
214#ifdef CONFIG_DEBUG_SG
215 BUG_ON(sg->sg_magic != SG_MAGIC);
216#endif
217 sg->page_link &= ~0x02;
218}
219
220/**
Jens Axboe82f66fb2007-10-22 17:07:37 +0200221 * sg_phys - Return physical address of an sg entry
222 * @sg: SG entry
223 *
224 * Description:
225 * This calls page_to_phys() on the page in this sg entry, and adds the
226 * sg offset. The caller must know that it is legal to call page_to_phys()
227 * on the sg page.
228 *
229 **/
Hugh Dickins85cdffc2007-10-25 09:55:05 +0200230static inline dma_addr_t sg_phys(struct scatterlist *sg)
Jens Axboe82f66fb2007-10-22 17:07:37 +0200231{
232 return page_to_phys(sg_page(sg)) + sg->offset;
233}
234
235/**
236 * sg_virt - Return virtual address of an sg entry
Jens Axboe18dabf42007-10-22 19:57:20 +0200237 * @sg: SG entry
Jens Axboe82f66fb2007-10-22 17:07:37 +0200238 *
239 * Description:
240 * This calls page_address() on the page in this sg entry, and adds the
241 * sg offset. The caller must know that the sg page has a valid virtual
242 * mapping.
243 *
244 **/
245static inline void *sg_virt(struct scatterlist *sg)
246{
247 return page_address(sg_page(sg)) + sg->offset;
248}
249
Maxim Levitsky2e484612012-09-27 12:45:28 +0200250int sg_nents(struct scatterlist *sg);
Tom Lendackycfaed102015-06-01 11:15:25 -0500251int sg_nents_for_len(struct scatterlist *sg, u64 len);
Jens Axboe0db92992007-11-30 09:16:50 +0100252struct scatterlist *sg_next(struct scatterlist *);
253struct scatterlist *sg_last(struct scatterlist *s, unsigned int);
254void sg_init_table(struct scatterlist *, unsigned int);
255void sg_init_one(struct scatterlist *, const void *, unsigned int);
Robert Jarzmikf8bcbe62015-08-08 10:44:10 +0200256int sg_split(struct scatterlist *in, const int in_mapped_nents,
257 const off_t skip, const int nb_splits,
258 const size_t *split_sizes,
259 struct scatterlist **out, int *out_mapped_nents,
260 gfp_t gfp_mask);
Jens Axboe0db92992007-11-30 09:16:50 +0100261
262typedef struct scatterlist *(sg_alloc_fn)(unsigned int, gfp_t);
263typedef void (sg_free_fn)(struct scatterlist *, unsigned int);
264
Christoph Hellwigc53c6d62014-04-15 14:38:31 +0200265void __sg_free_table(struct sg_table *, unsigned int, bool, sg_free_fn *);
Jens Axboe0db92992007-11-30 09:16:50 +0100266void sg_free_table(struct sg_table *);
Christoph Hellwigc53c6d62014-04-15 14:38:31 +0200267int __sg_alloc_table(struct sg_table *, unsigned int, unsigned int,
268 struct scatterlist *, gfp_t, sg_alloc_fn *);
Jens Axboe0db92992007-11-30 09:16:50 +0100269int sg_alloc_table(struct sg_table *, unsigned int, gfp_t);
Tomasz Stanislawskiefc42bc2012-06-18 09:25:01 +0200270int sg_alloc_table_from_pages(struct sg_table *sgt,
271 struct page **pages, unsigned int n_pages,
Tvrtko Ursulinc4860ad2017-07-31 19:55:08 +0100272 unsigned int offset, unsigned long size,
Tomasz Stanislawskiefc42bc2012-06-18 09:25:01 +0200273 gfp_t gfp_mask);
Jens Axboe0db92992007-11-30 09:16:50 +0100274
Dave Gordon386ecb12015-06-30 14:58:57 -0700275size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf,
276 size_t buflen, off_t skip, bool to_buffer);
277
FUJITA Tomonorib1adaf62008-03-18 00:15:03 +0900278size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
Dave Gordon2a1bf8f2015-06-30 14:58:54 -0700279 const void *buf, size_t buflen);
FUJITA Tomonorib1adaf62008-03-18 00:15:03 +0900280size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
281 void *buf, size_t buflen);
282
Akinobu Mitadf642ce2013-07-08 16:01:54 -0700283size_t sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents,
Dave Gordon2a1bf8f2015-06-30 14:58:54 -0700284 const void *buf, size_t buflen, off_t skip);
Akinobu Mitadf642ce2013-07-08 16:01:54 -0700285size_t sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents,
286 void *buf, size_t buflen, off_t skip);
Johannes Thumshirn0945e562017-06-07 11:45:28 +0200287size_t sg_zero_buffer(struct scatterlist *sgl, unsigned int nents,
288 size_t buflen, off_t skip);
Akinobu Mitadf642ce2013-07-08 16:01:54 -0700289
Jens Axboe0db92992007-11-30 09:16:50 +0100290/*
291 * Maximum number of entries that will be allocated in one piece, if
292 * a list larger than this is required then chaining will be utilized.
293 */
294#define SG_MAX_SINGLE_ALLOC (PAGE_SIZE / sizeof(struct scatterlist))
295
Imre Deaka321e912013-02-27 17:02:56 -0800296/*
Ming Lin9b1d6c82016-04-04 14:48:11 -0700297 * The maximum number of SG segments that we will put inside a
298 * scatterlist (unless chaining is used). Should ideally fit inside a
299 * single page, to avoid a higher order allocation. We could define this
300 * to SG_MAX_SINGLE_ALLOC to pack correctly at the highest order. The
301 * minimum value is 32
302 */
303#define SG_CHUNK_SIZE 128
304
305/*
306 * Like SG_CHUNK_SIZE, but for archs that have sg chaining. This limit
307 * is totally arbitrary, a setting of 2048 will get you at least 8mb ios.
308 */
309#ifdef CONFIG_ARCH_HAS_SG_CHAIN
310#define SG_MAX_SEGMENTS 2048
311#else
312#define SG_MAX_SEGMENTS SG_CHUNK_SIZE
313#endif
314
315#ifdef CONFIG_SG_POOL
316void sg_free_table_chained(struct sg_table *table, bool first_chunk);
317int sg_alloc_table_chained(struct sg_table *table, int nents,
318 struct scatterlist *first_chunk);
319#endif
320
321/*
Imre Deaka321e912013-02-27 17:02:56 -0800322 * sg page iterator
323 *
324 * Iterates over sg entries page-by-page. On each successful iteration,
Imre Deak2db76d72013-03-26 15:14:18 +0200325 * you can call sg_page_iter_page(@piter) and sg_page_iter_dma_address(@piter)
326 * to get the current page and its dma address. @piter->sg will point to the
327 * sg holding this page and @piter->sg_pgoffset to the page's page offset
328 * within the sg. The iteration will stop either when a maximum number of sg
329 * entries was reached or a terminating sg (sg_last(sg) == true) was reached.
Imre Deaka321e912013-02-27 17:02:56 -0800330 */
331struct sg_page_iter {
Imre Deaka321e912013-02-27 17:02:56 -0800332 struct scatterlist *sg; /* sg holding the page */
333 unsigned int sg_pgoffset; /* page offset within the sg */
334
335 /* these are internal states, keep away */
336 unsigned int __nents; /* remaining sg entries */
337 int __pg_advance; /* nr pages to advance at the
338 * next step */
339};
340
341bool __sg_page_iter_next(struct sg_page_iter *piter);
342void __sg_page_iter_start(struct sg_page_iter *piter,
343 struct scatterlist *sglist, unsigned int nents,
344 unsigned long pgoffset);
Imre Deak2db76d72013-03-26 15:14:18 +0200345/**
346 * sg_page_iter_page - get the current page held by the page iterator
347 * @piter: page iterator holding the page
348 */
349static inline struct page *sg_page_iter_page(struct sg_page_iter *piter)
350{
351 return nth_page(sg_page(piter->sg), piter->sg_pgoffset);
352}
353
354/**
355 * sg_page_iter_dma_address - get the dma address of the current page held by
356 * the page iterator.
357 * @piter: page iterator holding the page
358 */
359static inline dma_addr_t sg_page_iter_dma_address(struct sg_page_iter *piter)
360{
361 return sg_dma_address(piter->sg) + (piter->sg_pgoffset << PAGE_SHIFT);
362}
Imre Deaka321e912013-02-27 17:02:56 -0800363
364/**
365 * for_each_sg_page - iterate over the pages of the given sg list
366 * @sglist: sglist to iterate over
367 * @piter: page iterator to hold current page, sg, sg_pgoffset
368 * @nents: maximum number of sg entries to iterate over
369 * @pgoffset: starting page offset
370 */
371#define for_each_sg_page(sglist, piter, nents, pgoffset) \
372 for (__sg_page_iter_start((piter), (sglist), (nents), (pgoffset)); \
373 __sg_page_iter_next(piter);)
Tejun Heo137d3ed2008-07-19 23:03:35 +0900374
375/*
376 * Mapping sg iterator
377 *
378 * Iterates over sg entries mapping page-by-page. On each successful
379 * iteration, @miter->page points to the mapped page and
380 * @miter->length bytes of data can be accessed at @miter->addr. As
381 * long as an interation is enclosed between start and stop, the user
382 * is free to choose control structure and when to stop.
383 *
384 * @miter->consumed is set to @miter->length on each iteration. It
385 * can be adjusted if the user can't consume all the bytes in one go.
386 * Also, a stopped iteration can be resumed by calling next on it.
387 * This is useful when iteration needs to release all resources and
388 * continue later (e.g. at the next interrupt).
389 */
390
391#define SG_MITER_ATOMIC (1 << 0) /* use kmap_atomic */
Sebastian Andrzej Siewior6de7e3562009-06-18 10:19:12 +0200392#define SG_MITER_TO_SG (1 << 1) /* flush back to phys on unmap */
393#define SG_MITER_FROM_SG (1 << 2) /* nop */
Tejun Heo137d3ed2008-07-19 23:03:35 +0900394
395struct sg_mapping_iter {
396 /* the following three fields can be accessed directly */
397 struct page *page; /* currently mapped page */
398 void *addr; /* pointer to the mapped area */
399 size_t length; /* length of the mapped area */
400 size_t consumed; /* number of consumed bytes */
Imre Deak4225fc82013-02-27 17:02:57 -0800401 struct sg_page_iter piter; /* page iterator */
Tejun Heo137d3ed2008-07-19 23:03:35 +0900402
403 /* these are internal states, keep away */
Imre Deak4225fc82013-02-27 17:02:57 -0800404 unsigned int __offset; /* offset within page */
405 unsigned int __remaining; /* remaining bytes on page */
Tejun Heo137d3ed2008-07-19 23:03:35 +0900406 unsigned int __flags;
407};
408
409void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
410 unsigned int nents, unsigned int flags);
Ming Lei0d6077f2013-11-26 12:43:37 +0800411bool sg_miter_skip(struct sg_mapping_iter *miter, off_t offset);
Tejun Heo137d3ed2008-07-19 23:03:35 +0900412bool sg_miter_next(struct sg_mapping_iter *miter);
413void sg_miter_stop(struct sg_mapping_iter *miter);
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415#endif /* _LINUX_SCATTERLIST_H */