2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6 * Copyright (c) 2015 Matthew Dillon <dillon@backplane.com>
7 * Copyright (c) 2016 Matt Macy <mmacy@nextbsd.org>
8 * Copyright (c) 2017 François Tigeot <ftigeot@wolfpond.org>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice unmodified, this list of conditions, and the following
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #ifndef _LINUX_SCATTERLIST_H_
34 #define _LINUX_SCATTERLIST_H_
36 #include <linux/string.h>
37 #include <linux/types.h>
38 #include <linux/bug.h>
45 * If flags bit 0 is set, then the sg field contains a pointer to the next sg
46 * table list. Otherwise the next entry is at sg + 1, can be determined using
47 * the sg_is_chain() function.
49 * If flags bit 1 is set, then this sg entry is the last element in a list,
50 * can be determined using the sg_is_last() function.
59 struct scatterlist *sg;
63 dma_addr_t dma_address;
68 struct scatterlist *sgl; /* the list */
69 unsigned int nents; /* number of mapped entries */
70 unsigned int orig_nents; /* original size of list */
74 struct scatterlist *sg;
75 unsigned int sg_pgoffset; /* page index */
83 * Maximum number of entries that will be allocated in one piece, if
84 * a list larger than this is required then chaining will be utilized.
86 #define SG_MAX_SINGLE_ALLOC (PAGE_SIZE / sizeof(struct scatterlist))
88 #define sg_dma_address(sg) (sg)->dma_address
89 #define sg_dma_len(sg) (sg)->length
90 #define sg_page(sg) (sg)->sl_un.page
91 #define sg_scatternext(sg) (sg)->sl_un.sg
97 sg_set_page(struct scatterlist *sg, struct page *page, unsigned int len,
101 sg_dma_len(sg) = len;
103 if (offset > PAGE_SIZE)
104 panic("sg_set_page: Invalid offset %d\n", offset);
108 sg_init_table(struct scatterlist *sg, unsigned int nents)
110 bzero(sg, sizeof(*sg) * nents);
111 sg[nents - 1].flags = SG_END;
114 static inline struct scatterlist *
115 sg_next(struct scatterlist *sg)
117 if (sg->flags & SG_END)
120 if (sg->flags & SG_CHAIN)
121 sg = sg_scatternext(sg);
125 static inline vm_paddr_t
126 sg_phys(struct scatterlist *sg)
128 return ((struct vm_page *)sg_page(sg))->phys_addr + sg->offset;
132 * sg_chain - Chain two sglists together
133 * @prv: First scatterlist
134 * @prv_nents: Number of entries in prv
135 * @sgl: Second scatterlist
138 * Links @prv@ and @sgl@ together, to form a longer scatterlist.
142 sg_chain(struct scatterlist *prv, unsigned int prv_nents,
143 struct scatterlist *sgl)
146 * offset and length are unused for chain entry. Clear them.
148 struct scatterlist *sg = &prv[prv_nents - 1];
154 * Indicate a link pointer, and set the link to the second list.
156 sg->flags = SG_CHAIN;
161 * sg_mark_end - Mark the end of the scatterlist
162 * @sg: SG entryScatterlist
165 * Marks the passed in sg entry as the termination point for the sg
166 * table. A call to sg_next() on this entry will return NULL.
169 static inline void sg_mark_end(struct scatterlist *sg)
175 * __sg_free_table - Free a previously mapped sg table
176 * @table: The sg table header to use
177 * @max_ents: The maximum number of entries per single scatterlist
180 * Free an sg table previously allocated and setup with
181 * __sg_alloc_table(). The @max_ents value must be identical to
182 * that previously used with __sg_alloc_table().
185 void __sg_free_table(struct sg_table *table, unsigned int max_ents);
188 * sg_free_table - Free a previously allocated sg table
189 * @table: The mapped sg table header
193 sg_free_table(struct sg_table *table)
195 __sg_free_table(table, SG_MAX_SINGLE_ALLOC);
198 int __sg_alloc_table(struct sg_table *table, unsigned int nents,
199 unsigned int max_ents, gfp_t gfp_mask);
202 * sg_alloc_table - Allocate and initialize an sg table
203 * @table: The sg table header to use
204 * @nents: Number of entries in sg list
205 * @gfp_mask: GFP allocation mask
208 * Allocate and initialize an sg table. If @nents@ is larger than
209 * SG_MAX_SINGLE_ALLOC a chained sg table will be setup.
214 sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
218 ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
221 __sg_free_table(table, SG_MAX_SINGLE_ALLOC);
227 sg_nents(struct scatterlist *sg)
230 for (nents = 0; sg; sg = sg_next(sg))
236 __sg_page_iter_start(struct sg_page_iter *piter,
237 struct scatterlist *sglist, unsigned int nents,
238 unsigned long pgoffset)
240 piter->__pg_advance = 0;
241 piter->__nents = nents;
244 piter->sg_pgoffset = pgoffset;
248 * Iterate pages in sg list.
251 _sg_iter_next(struct sg_page_iter *iter)
253 struct scatterlist *sg;
254 unsigned int pgcount;
257 pgcount = (sg->offset + sg->length + PAGE_MASK) >> PAGE_SHIFT;
260 while (iter->sg_pgoffset >= pgcount) {
261 iter->sg_pgoffset -= pgcount;
264 if (sg == NULL || iter->maxents == 0)
266 pgcount = (sg->offset + sg->length + PAGE_MASK) >> PAGE_SHIFT;
272 sg_page_count(struct scatterlist *sg)
274 return PAGE_ALIGN(sg->offset + sg->length) >> PAGE_SHIFT;
278 __sg_page_iter_next(struct sg_page_iter *piter)
280 if (piter->__nents == 0)
282 if (piter->sg == NULL)
285 piter->sg_pgoffset += piter->__pg_advance;
286 piter->__pg_advance = 1;
288 while (piter->sg_pgoffset >= sg_page_count(piter->sg)) {
289 piter->sg_pgoffset -= sg_page_count(piter->sg);
290 piter->sg = sg_next(piter->sg);
291 if (--piter->__nents == 0)
293 if (piter->sg == NULL)
300 * NOTE: pgoffset is really a page index, not a byte offset.
303 _sg_iter_init(struct scatterlist *sgl, struct sg_page_iter *iter,
304 unsigned int nents, unsigned long pgoffset)
308 * Nominal case. Note subtract 1 from starting page index
309 * for initial _sg_iter_next() call.
312 iter->sg_pgoffset = pgoffset - 1;
313 iter->maxents = nents;
320 iter->sg_pgoffset = 0;
325 static inline struct page *
326 sg_page_iter_page(struct sg_page_iter *piter)
328 return nth_page(sg_page(piter->sg), piter->sg_pgoffset);
331 static inline dma_addr_t
332 sg_page_iter_dma_address(struct sg_page_iter *spi)
334 return spi->sg->dma_address + (spi->sg_pgoffset << PAGE_SHIFT);
337 #define for_each_sg_page(sgl, iter, nents, pgoffset) \
338 for (_sg_iter_init(sgl, iter, nents, pgoffset); \
339 (iter)->sg; _sg_iter_next(iter))
341 #define for_each_sg(sglist, sg, sgmax, _itr) \
342 for (_itr = 0, sg = (sglist); _itr < (sgmax); _itr++, sg = sg_next(sg))
346 * XXX please review these
348 size_t sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents,
349 const void *buf, size_t buflen, off_t skip);
352 sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
353 const char *buf, size_t buflen)
355 return (sg_pcopy_from_buffer(sgl, nents, buf, buflen, 0));
358 size_t sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents,
359 void *buf, size_t buflen, off_t skip);
362 sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
363 char *buf, size_t buflen)
366 return (sg_pcopy_to_buffer(sgl, nents, buf, buflen, 0));
370 sg_is_last(struct scatterlist *sg)
372 return (sg->flags & SG_END);
376 sg_is_chain(struct scatterlist *sg)
378 return (sg->flags & SG_CHAIN);
381 static inline struct scatterlist *
382 sg_chain_ptr(struct scatterlist *sg)
387 #endif /* _LINUX_SCATTERLIST_H_ */