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>
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.
31 #ifndef _LINUX_SCATTERLIST_H_
32 #define _LINUX_SCATTERLIST_H_
34 #include <linux/bug.h>
39 * If flags bit 0 is set, then the sg field contains a pointer to the next sg
40 * table list. Otherwise the next entry is at sg + 1, can be determined using
41 * the sg_is_chain() function.
43 * If flags bit 1 is set, then this sg entry is the last element in a list,
44 * can be determined using the sg_is_last() function.
53 struct scatterlist *sg;
62 struct scatterlist *sgl; /* the list */
63 unsigned int nents; /* number of mapped entries */
64 unsigned int orig_nents; /* original size of list */
68 struct scatterlist *sg;
69 unsigned int sg_pgoffset; /* page index */
74 * Maximum number of entries that will be allocated in one piece, if
75 * a list larger than this is required then chaining will be utilized.
77 #define SG_MAX_SINGLE_ALLOC (PAGE_SIZE / sizeof(struct scatterlist))
79 #define sg_dma_address(sg) (sg)->address
80 #define sg_dma_len(sg) (sg)->length
81 #define sg_page(sg) (sg)->sl_un.page
82 #define sg_scatternext(sg) (sg)->sl_un.sg
88 sg_set_page(struct scatterlist *sg, struct vm_page *page, unsigned int len,
94 if (offset > PAGE_SIZE)
95 panic("sg_set_page: Invalid offset %d\n", offset);
100 sg_set_buf(struct scatterlist *sg, const void *buf, unsigned int buflen)
102 sg_set_page(sg, virt_to_page(buf), buflen,
103 ((uintptr_t)buf) & ~PAGE_MASK);
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);
126 static inline vm_paddr_t
127 sg_phys(struct scatterlist *sg)
129 return sg_page(sg)->phys_addr + sg->offset;
134 * sg_chain - Chain two sglists together
135 * @prv: First scatterlist
136 * @prv_nents: Number of entries in prv
137 * @sgl: Second scatterlist
140 * Links @prv@ and @sgl@ together, to form a longer scatterlist.
144 sg_chain(struct scatterlist *prv, unsigned int prv_nents,
145 struct scatterlist *sgl)
148 * offset and length are unused for chain entry. Clear them.
150 struct scatterlist *sg = &prv[prv_nents - 1];
156 * Indicate a link pointer, and set the link to the second list.
158 sg->flags = SG_CHAIN;
163 * sg_mark_end - Mark the end of the scatterlist
164 * @sg: SG entryScatterlist
167 * Marks the passed in sg entry as the termination point for the sg
168 * table. A call to sg_next() on this entry will return NULL.
171 static inline void sg_mark_end(struct scatterlist *sg)
177 * __sg_free_table - Free a previously mapped sg table
178 * @table: The sg table header to use
179 * @max_ents: The maximum number of entries per single scatterlist
182 * Free an sg table previously allocated and setup with
183 * __sg_alloc_table(). The @max_ents value must be identical to
184 * that previously used with __sg_alloc_table().
188 __sg_free_table(struct sg_table *table, unsigned int max_ents)
190 struct scatterlist *sgl, *next;
192 if (unlikely(!table->sgl))
196 while (table->orig_nents) {
197 unsigned int alloc_size = table->orig_nents;
198 unsigned int sg_size;
201 * If we have more than max_ents segments left,
202 * then assign 'next' to the sg table after the current one.
203 * sg_size is then one less than alloc size, since the last
204 * element is the chain pointer.
206 if (alloc_size > max_ents) {
207 next = sgl[max_ents - 1].sl_un.sg;
208 alloc_size = max_ents;
209 sg_size = alloc_size - 1;
211 sg_size = alloc_size;
215 table->orig_nents -= sg_size;
224 * sg_free_table - Free a previously allocated sg table
225 * @table: The mapped sg table header
229 sg_free_table(struct sg_table *table)
231 __sg_free_table(table, SG_MAX_SINGLE_ALLOC);
235 * __sg_alloc_table - Allocate and initialize an sg table with given allocator
236 * @table: The sg table header to use
237 * @nents: Number of entries in sg list
238 * @max_ents: The maximum number of entries the allocator returns per call
239 * @gfp_mask: GFP allocation mask
242 * This function returns a @table @nents long. The allocator is
243 * defined to return scatterlist chunks of maximum size @max_ents.
244 * Thus if @nents is bigger than @max_ents, the scatterlists will be
245 * chained in units of @max_ents.
248 * If this function returns non-0 (eg failure), the caller must call
249 * __sg_free_table() to cleanup any leftover allocations.
253 __sg_alloc_table(struct sg_table *table, unsigned int nents,
254 unsigned int max_ents, gfp_t gfp_mask)
256 struct scatterlist *sg, *prv;
259 memset(table, 0, sizeof(*table));
266 unsigned int sg_size, alloc_size = left;
268 if (alloc_size > max_ents) {
269 alloc_size = max_ents;
270 sg_size = alloc_size - 1;
272 sg_size = alloc_size;
276 sg = kmalloc(alloc_size * sizeof(struct scatterlist), M_DRM, gfp_mask);
279 * Adjust entry count to reflect that the last
280 * entry of the previous table won't be used for
281 * linkage. Without this, sg_kfree() may get
285 table->nents = ++table->orig_nents;
290 sg_init_table(sg, alloc_size);
291 table->nents = table->orig_nents += sg_size;
294 * If this is the first mapping, assign the sg table header.
295 * If this is not the first mapping, chain previous part.
298 sg_chain(prv, max_ents, sg);
303 * If no more entries after this one, mark the end
306 sg_mark_end(&sg[sg_size - 1]);
315 * sg_alloc_table - Allocate and initialize an sg table
316 * @table: The sg table header to use
317 * @nents: Number of entries in sg list
318 * @gfp_mask: GFP allocation mask
321 * Allocate and initialize an sg table. If @nents@ is larger than
322 * SG_MAX_SINGLE_ALLOC a chained sg table will be setup.
327 sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
331 ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
334 __sg_free_table(table, SG_MAX_SINGLE_ALLOC);
340 * Iterate pages in sg list.
343 _sg_iter_next(struct sg_page_iter *iter)
345 struct scatterlist *sg;
346 unsigned int pgcount;
349 pgcount = (sg->offset + sg->length + PAGE_MASK) >> PAGE_SHIFT;
352 while (iter->sg_pgoffset >= pgcount) {
353 iter->sg_pgoffset -= pgcount;
356 if (sg == NULL || iter->maxents == 0)
358 pgcount = (sg->offset + sg->length + PAGE_MASK) >> PAGE_SHIFT;
364 * NOTE: pgoffset is really a page index, not a byte offset.
367 _sg_iter_init(struct scatterlist *sgl, struct sg_page_iter *iter,
368 unsigned int nents, unsigned long pgoffset)
372 * Nominal case. Note subtract 1 from starting page index
373 * for initial _sg_iter_next() call.
376 iter->sg_pgoffset = pgoffset - 1;
377 iter->maxents = nents;
384 iter->sg_pgoffset = 0;
389 static inline dma_addr_t
390 sg_page_iter_dma_address(struct sg_page_iter *spi)
392 return spi->sg->address + (spi->sg_pgoffset << PAGE_SHIFT);
395 #define for_each_sg_page(sgl, iter, nents, pgoffset) \
396 for (_sg_iter_init(sgl, iter, nents, pgoffset); \
397 (iter)->sg; _sg_iter_next(iter))
399 #define for_each_sg(sglist, sg, sgmax, _itr) \
400 for (_itr = 0, sg = (sglist); _itr < (sgmax); _itr++, sg = sg_next(sg))
402 #endif /* _LINUX_SCATTERLIST_H_ */