2 * Copyright (C) 2007 Jens Axboe <jens.axboe@oracle.com>
4 * Scatterlist handling helpers.
6 * This source code is licensed under the GNU General Public License,
7 * Version 2. See the file COPYING for more details.
9 #include <linux/module.h>
10 #include <linux/scatterlist.h>
13 * sg_next - return the next scatterlist entry in a list
14 * @sg: The current sg entry
17 * Usually the next entry will be @sg@ + 1, but if this sg element is part
18 * of a chained scatterlist, it could jump to the start of a new
22 struct scatterlist *sg_next(struct scatterlist *sg)
24 #ifdef CONFIG_DEBUG_SG
25 BUG_ON(sg->sg_magic != SG_MAGIC);
31 if (unlikely(sg_is_chain(sg)))
32 sg = sg_chain_ptr(sg);
36 EXPORT_SYMBOL(sg_next);
39 * sg_last - return the last scatterlist entry in a list
40 * @sgl: First entry in the scatterlist
41 * @nents: Number of entries in the scatterlist
44 * Should only be used casually, it (currently) scans the entire list
45 * to get the last entry.
47 * Note that the @sgl@ pointer passed in need not be the first one,
48 * the important bit is that @nents@ denotes the number of entries that
52 struct scatterlist *sg_last(struct scatterlist *sgl, unsigned int nents)
54 #ifndef ARCH_HAS_SG_CHAIN
55 struct scatterlist *ret = &sgl[nents - 1];
57 struct scatterlist *sg, *ret = NULL;
60 for_each_sg(sgl, sg, nents, i)
64 #ifdef CONFIG_DEBUG_SG
65 BUG_ON(sgl[0].sg_magic != SG_MAGIC);
66 BUG_ON(!sg_is_last(ret));
70 EXPORT_SYMBOL(sg_last);
73 * sg_init_table - Initialize SG table
75 * @nents: Number of entries in table
78 * If this is part of a chained sg table, sg_mark_end() should be
79 * used only on the last table part.
82 void sg_init_table(struct scatterlist *sgl, unsigned int nents)
84 memset(sgl, 0, sizeof(*sgl) * nents);
85 #ifdef CONFIG_DEBUG_SG
88 for (i = 0; i < nents; i++)
89 sgl[i].sg_magic = SG_MAGIC;
92 sg_mark_end(&sgl[nents - 1]);
94 EXPORT_SYMBOL(sg_init_table);
97 * sg_init_one - Initialize a single entry sg list
99 * @buf: Virtual address for IO
103 void sg_init_one(struct scatterlist *sg, const void *buf, unsigned int buflen)
105 sg_init_table(sg, 1);
106 sg_set_buf(sg, buf, buflen);
108 EXPORT_SYMBOL(sg_init_one);
111 * The default behaviour of sg_alloc_table() is to use these kmalloc/kfree
114 static struct scatterlist *sg_kmalloc(unsigned int nents, gfp_t gfp_mask)
116 if (nents == SG_MAX_SINGLE_ALLOC)
117 return (struct scatterlist *) __get_free_page(gfp_mask);
119 return kmalloc(nents * sizeof(struct scatterlist), gfp_mask);
122 static void sg_kfree(struct scatterlist *sg, unsigned int nents)
124 if (nents == SG_MAX_SINGLE_ALLOC)
125 free_page((unsigned long) sg);
131 * __sg_free_table - Free a previously mapped sg table
132 * @table: The sg table header to use
133 * @free_fn: Free function
136 * Free an sg table previously allocated and setup with __sg_alloc_table().
139 void __sg_free_table(struct sg_table *table, sg_free_fn *free_fn)
141 struct scatterlist *sgl, *next;
143 if (unlikely(!table->sgl))
147 while (table->orig_nents) {
148 unsigned int alloc_size = table->orig_nents;
149 unsigned int sg_size;
152 * If we have more than SG_MAX_SINGLE_ALLOC segments left,
153 * then assign 'next' to the sg table after the current one.
154 * sg_size is then one less than alloc size, since the last
155 * element is the chain pointer.
157 if (alloc_size > SG_MAX_SINGLE_ALLOC) {
158 next = sg_chain_ptr(&sgl[SG_MAX_SINGLE_ALLOC - 1]);
159 alloc_size = SG_MAX_SINGLE_ALLOC;
160 sg_size = alloc_size - 1;
162 sg_size = alloc_size;
166 table->orig_nents -= sg_size;
167 free_fn(sgl, alloc_size);
173 EXPORT_SYMBOL(__sg_free_table);
176 * sg_free_table - Free a previously allocated sg table
177 * @table: The mapped sg table header
180 void sg_free_table(struct sg_table *table)
182 __sg_free_table(table, sg_kfree);
184 EXPORT_SYMBOL(sg_free_table);
187 * __sg_alloc_table - Allocate and initialize an sg table with given allocator
188 * @table: The sg table header to use
189 * @nents: Number of entries in sg list
190 * @gfp_mask: GFP allocation mask
191 * @alloc_fn: Allocator to use
194 * If this function returns non-0 (eg failure), the caller must call
195 * __sg_free_table() to cleanup any leftover allocations.
198 int __sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask,
199 sg_alloc_fn *alloc_fn)
201 struct scatterlist *sg, *prv;
204 #ifndef ARCH_HAS_SG_CHAIN
205 BUG_ON(nents > SG_MAX_SINGLE_ALLOC);
208 memset(table, 0, sizeof(*table));
213 unsigned int sg_size, alloc_size = left;
215 if (alloc_size > SG_MAX_SINGLE_ALLOC) {
216 alloc_size = SG_MAX_SINGLE_ALLOC;
217 sg_size = alloc_size - 1;
219 sg_size = alloc_size;
223 sg = alloc_fn(alloc_size, gfp_mask);
227 sg_init_table(sg, alloc_size);
228 table->nents = table->orig_nents += sg_size;
231 * If this is the first mapping, assign the sg table header.
232 * If this is not the first mapping, chain previous part.
235 sg_chain(prv, SG_MAX_SINGLE_ALLOC, sg);
240 * If no more entries after this one, mark the end
243 sg_mark_end(&sg[sg_size - 1]);
246 * only really needed for mempool backed sg allocations (like
247 * SCSI), a possible improvement here would be to pass the
248 * table pointer into the allocator and let that clear these
251 gfp_mask &= ~__GFP_WAIT;
252 gfp_mask |= __GFP_HIGH;
258 EXPORT_SYMBOL(__sg_alloc_table);
261 * sg_alloc_table - Allocate and initialize an sg table
262 * @table: The sg table header to use
263 * @nents: Number of entries in sg list
264 * @gfp_mask: GFP allocation mask
267 * Allocate and initialize an sg table. If @nents@ is larger than
268 * SG_MAX_SINGLE_ALLOC a chained sg table will be setup.
271 int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
275 ret = __sg_alloc_table(table, nents, gfp_mask, sg_kmalloc);
277 __sg_free_table(table, sg_kfree);
281 EXPORT_SYMBOL(sg_alloc_table);