2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013-2015 Mellanox Technologies, Ltd.
6 * Copyright (c) 2015 Matthew Dillon <dillon@backplane.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifndef _LINUX_SCATTERLIST_H_
33 #define _LINUX_SCATTERLIST_H_
35 #include <linux/page.h>
36 #include <linux/slab.h>
41 struct scatterlist *sg;
50 struct scatterlist *sgl;
52 unsigned int orig_nents;
56 struct scatterlist *sg;
57 unsigned int sg_pgoffset;
61 #define SG_MAX_SINGLE_ALLOC (PAGE_SIZE / sizeof(struct scatterlist))
63 #define sg_dma_address(sg) (sg)->address
64 #define sg_dma_len(sg) (sg)->length
65 #define sg_page(sg) (sg)->sl_un.page
66 #define sg_scatternext(sg) (sg)->sl_un.sg
72 sg_set_page(struct scatterlist *sg, struct page *page, unsigned int len,
78 if (offset > PAGE_SIZE)
79 panic("sg_set_page: Invalid offset %d\n", offset);
83 sg_set_buf(struct scatterlist *sg, const void *buf, unsigned int buflen)
85 sg_set_page(sg, virt_to_page(buf), buflen,
86 ((uintptr_t)buf) & (PAGE_SIZE - 1));
90 sg_init_table(struct scatterlist *sg, unsigned int nents)
92 bzero(sg, sizeof(*sg) * nents);
93 sg[nents - 1].flags = SG_END;
96 static inline struct scatterlist *
97 sg_next(struct scatterlist *sg)
99 if (sg->flags & SG_END)
102 if (sg->flags & SG_CHAIN)
103 sg = sg_scatternext(sg);
107 static inline vm_paddr_t
108 sg_phys(struct scatterlist *sg)
110 return sg_page(sg)->phys_addr + sg->offset;
114 sg_chain(struct scatterlist *prv, unsigned int prv_nents,
115 struct scatterlist *sgl)
117 struct scatterlist *sg = &prv[prv_nents - 1];
121 sg->flags = SG_CHAIN;
126 sg_mark_end(struct scatterlist *sg)
132 __sg_free_table(struct sg_table *table, unsigned int max_ents)
134 struct scatterlist *sgl, *next;
136 if (unlikely(!table->sgl))
140 while (table->orig_nents) {
141 unsigned int alloc_size = table->orig_nents;
142 unsigned int sg_size;
144 if (alloc_size > max_ents) {
145 next = sgl[max_ents - 1].sl_un.sg;
146 alloc_size = max_ents;
147 sg_size = alloc_size - 1;
149 sg_size = alloc_size;
153 table->orig_nents -= sg_size;
162 sg_free_table(struct sg_table *table)
164 __sg_free_table(table, SG_MAX_SINGLE_ALLOC);
168 __sg_alloc_table(struct sg_table *table, unsigned int nents,
169 unsigned int max_ents, gfp_t gfp_mask)
171 struct scatterlist *sg, *prv;
174 memset(table, 0, sizeof(*table));
181 unsigned int sg_size;
182 unsigned int alloc_size = left;
184 if (alloc_size > max_ents) {
185 alloc_size = max_ents;
186 sg_size = alloc_size - 1;
188 sg_size = alloc_size;
192 sg = kmalloc(alloc_size * sizeof(struct scatterlist), gfp_mask);
195 table->nents = ++table->orig_nents;
199 sg_init_table(sg, alloc_size);
200 table->nents = table->orig_nents += sg_size;
203 sg_chain(prv, max_ents, sg);
208 sg_mark_end(&sg[sg_size - 1]);
217 sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
221 ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
224 __sg_free_table(table, SG_MAX_SINGLE_ALLOC);
230 _sg_iter_next(struct sg_page_iter *iter)
232 struct scatterlist *sg;
233 unsigned int pgcount;
236 pgcount = (sg->offset + sg->length + PAGE_SIZE - 1) >> PAGE_SHIFT;
239 while (iter->sg_pgoffset >= pgcount) {
240 iter->sg_pgoffset -= pgcount;
243 if (sg == NULL || iter->maxents == 0)
245 pgcount = (sg->offset + sg->length + PAGE_SIZE - 1) >> PAGE_SHIFT;
251 _sg_iter_init(struct scatterlist *sgl, struct sg_page_iter *iter,
252 unsigned int nents, unsigned long pgoffset)
256 iter->sg_pgoffset = pgoffset - 1;
257 iter->maxents = nents;
261 iter->sg_pgoffset = 0;
266 static inline dma_addr_t
267 sg_page_iter_dma_address(struct sg_page_iter *spi)
269 return spi->sg->address + (spi->sg_pgoffset << PAGE_SHIFT);
272 #define for_each_sg_page(sgl, iter, nents, pgoffset) \
273 for (_sg_iter_init(sgl, iter, nents, pgoffset); \
274 (iter)->sg; _sg_iter_next(iter))
276 #define for_each_sg(sglist, sg, sgmax, _itr) \
277 for (_itr = 0, sg = (sglist); _itr < (sgmax); _itr++, sg = sg_next(sg))
279 #endif /* _LINUX_SCATTERLIST_H_ */