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>
11 #include <linux/highmem.h>
14 * sg_next - return the next scatterlist entry in a list
15 * @sg: The current sg entry
18 * Usually the next entry will be @sg@ + 1, but if this sg element is part
19 * of a chained scatterlist, it could jump to the start of a new
23 struct scatterlist
*sg_next(struct scatterlist
*sg
)
25 #ifdef CONFIG_DEBUG_SG
26 BUG_ON(sg
->sg_magic
!= SG_MAGIC
);
32 if (unlikely(sg_is_chain(sg
)))
33 sg
= sg_chain_ptr(sg
);
37 EXPORT_SYMBOL(sg_next
);
40 * sg_last - return the last scatterlist entry in a list
41 * @sgl: First entry in the scatterlist
42 * @nents: Number of entries in the scatterlist
45 * Should only be used casually, it (currently) scans the entire list
46 * to get the last entry.
48 * Note that the @sgl@ pointer passed in need not be the first one,
49 * the important bit is that @nents@ denotes the number of entries that
53 struct scatterlist
*sg_last(struct scatterlist
*sgl
, unsigned int nents
)
55 #ifndef ARCH_HAS_SG_CHAIN
56 struct scatterlist
*ret
= &sgl
[nents
- 1];
58 struct scatterlist
*sg
, *ret
= NULL
;
61 for_each_sg(sgl
, sg
, nents
, i
)
65 #ifdef CONFIG_DEBUG_SG
66 BUG_ON(sgl
[0].sg_magic
!= SG_MAGIC
);
67 BUG_ON(!sg_is_last(ret
));
71 EXPORT_SYMBOL(sg_last
);
74 * sg_init_table - Initialize SG table
76 * @nents: Number of entries in table
79 * If this is part of a chained sg table, sg_mark_end() should be
80 * used only on the last table part.
83 void sg_init_table(struct scatterlist
*sgl
, unsigned int nents
)
85 memset(sgl
, 0, sizeof(*sgl
) * nents
);
86 #ifdef CONFIG_DEBUG_SG
89 for (i
= 0; i
< nents
; i
++)
90 sgl
[i
].sg_magic
= SG_MAGIC
;
93 sg_mark_end(&sgl
[nents
- 1]);
95 EXPORT_SYMBOL(sg_init_table
);
98 * sg_init_one - Initialize a single entry sg list
100 * @buf: Virtual address for IO
104 void sg_init_one(struct scatterlist
*sg
, const void *buf
, unsigned int buflen
)
106 sg_init_table(sg
, 1);
107 sg_set_buf(sg
, buf
, buflen
);
109 EXPORT_SYMBOL(sg_init_one
);
112 * The default behaviour of sg_alloc_table() is to use these kmalloc/kfree
115 static struct scatterlist
*sg_kmalloc(unsigned int nents
, gfp_t gfp_mask
)
117 if (nents
== SG_MAX_SINGLE_ALLOC
)
118 return (struct scatterlist
*) __get_free_page(gfp_mask
);
120 return kmalloc(nents
* sizeof(struct scatterlist
), gfp_mask
);
123 static void sg_kfree(struct scatterlist
*sg
, unsigned int nents
)
125 if (nents
== SG_MAX_SINGLE_ALLOC
)
126 free_page((unsigned long) sg
);
132 * __sg_free_table - Free a previously mapped sg table
133 * @table: The sg table header to use
134 * @max_ents: The maximum number of entries per single scatterlist
135 * @free_fn: Free function
138 * Free an sg table previously allocated and setup with
139 * __sg_alloc_table(). The @max_ents value must be identical to
140 * that previously used with __sg_alloc_table().
143 void __sg_free_table(struct sg_table
*table
, unsigned int max_ents
,
146 struct scatterlist
*sgl
, *next
;
148 if (unlikely(!table
->sgl
))
152 while (table
->orig_nents
) {
153 unsigned int alloc_size
= table
->orig_nents
;
154 unsigned int sg_size
;
157 * If we have more than max_ents segments left,
158 * then assign 'next' to the sg table after the current one.
159 * sg_size is then one less than alloc size, since the last
160 * element is the chain pointer.
162 if (alloc_size
> max_ents
) {
163 next
= sg_chain_ptr(&sgl
[max_ents
- 1]);
164 alloc_size
= max_ents
;
165 sg_size
= alloc_size
- 1;
167 sg_size
= alloc_size
;
171 table
->orig_nents
-= sg_size
;
172 free_fn(sgl
, alloc_size
);
178 EXPORT_SYMBOL(__sg_free_table
);
181 * sg_free_table - Free a previously allocated sg table
182 * @table: The mapped sg table header
185 void sg_free_table(struct sg_table
*table
)
187 __sg_free_table(table
, SG_MAX_SINGLE_ALLOC
, sg_kfree
);
189 EXPORT_SYMBOL(sg_free_table
);
192 * __sg_alloc_table - Allocate and initialize an sg table with given allocator
193 * @table: The sg table header to use
194 * @nents: Number of entries in sg list
195 * @max_ents: The maximum number of entries the allocator returns per call
196 * @gfp_mask: GFP allocation mask
197 * @alloc_fn: Allocator to use
200 * This function returns a @table @nents long. The allocator is
201 * defined to return scatterlist chunks of maximum size @max_ents.
202 * Thus if @nents is bigger than @max_ents, the scatterlists will be
203 * chained in units of @max_ents.
206 * If this function returns non-0 (eg failure), the caller must call
207 * __sg_free_table() to cleanup any leftover allocations.
210 int __sg_alloc_table(struct sg_table
*table
, unsigned int nents
,
211 unsigned int max_ents
, gfp_t gfp_mask
,
212 sg_alloc_fn
*alloc_fn
)
214 struct scatterlist
*sg
, *prv
;
217 #ifndef ARCH_HAS_SG_CHAIN
218 BUG_ON(nents
> max_ents
);
221 memset(table
, 0, sizeof(*table
));
226 unsigned int sg_size
, alloc_size
= left
;
228 if (alloc_size
> max_ents
) {
229 alloc_size
= max_ents
;
230 sg_size
= alloc_size
- 1;
232 sg_size
= alloc_size
;
236 sg
= alloc_fn(alloc_size
, gfp_mask
);
240 sg_init_table(sg
, alloc_size
);
241 table
->nents
= table
->orig_nents
+= sg_size
;
244 * If this is the first mapping, assign the sg table header.
245 * If this is not the first mapping, chain previous part.
248 sg_chain(prv
, max_ents
, sg
);
253 * If no more entries after this one, mark the end
256 sg_mark_end(&sg
[sg_size
- 1]);
259 * only really needed for mempool backed sg allocations (like
260 * SCSI), a possible improvement here would be to pass the
261 * table pointer into the allocator and let that clear these
264 gfp_mask
&= ~__GFP_WAIT
;
265 gfp_mask
|= __GFP_HIGH
;
271 EXPORT_SYMBOL(__sg_alloc_table
);
274 * sg_alloc_table - Allocate and initialize an sg table
275 * @table: The sg table header to use
276 * @nents: Number of entries in sg list
277 * @gfp_mask: GFP allocation mask
280 * Allocate and initialize an sg table. If @nents@ is larger than
281 * SG_MAX_SINGLE_ALLOC a chained sg table will be setup.
284 int sg_alloc_table(struct sg_table
*table
, unsigned int nents
, gfp_t gfp_mask
)
288 ret
= __sg_alloc_table(table
, nents
, SG_MAX_SINGLE_ALLOC
,
289 gfp_mask
, sg_kmalloc
);
291 __sg_free_table(table
, SG_MAX_SINGLE_ALLOC
, sg_kfree
);
295 EXPORT_SYMBOL(sg_alloc_table
);
298 * sg_miter_start - start mapping iteration over a sg list
299 * @miter: sg mapping iter to be started
300 * @sgl: sg list to iterate over
301 * @nents: number of sg entries
304 * Starts mapping iterator @miter.
309 void sg_miter_start(struct sg_mapping_iter
*miter
, struct scatterlist
*sgl
,
310 unsigned int nents
, unsigned int flags
)
312 memset(miter
, 0, sizeof(struct sg_mapping_iter
));
315 miter
->__nents
= nents
;
317 WARN_ON(!(flags
& (SG_MITER_TO_SG
| SG_MITER_FROM_SG
)));
318 miter
->__flags
= flags
;
320 EXPORT_SYMBOL(sg_miter_start
);
323 * sg_miter_next - proceed mapping iterator to the next mapping
324 * @miter: sg mapping iter to proceed
327 * Proceeds @miter@ to the next mapping. @miter@ should have been
328 * started using sg_miter_start(). On successful return,
329 * @miter@->page, @miter@->addr and @miter@->length point to the
333 * IRQ disabled if SG_MITER_ATOMIC. IRQ must stay disabled till
334 * @miter@ is stopped. May sleep if !SG_MITER_ATOMIC.
337 * true if @miter contains the next mapping. false if end of sg
340 bool sg_miter_next(struct sg_mapping_iter
*miter
)
342 unsigned int off
, len
;
344 /* check for end and drop resources from the last iteration */
348 sg_miter_stop(miter
);
350 /* get to the next sg if necessary. __offset is adjusted by stop */
351 while (miter
->__offset
== miter
->__sg
->length
) {
352 if (--miter
->__nents
) {
353 miter
->__sg
= sg_next(miter
->__sg
);
359 /* map the next page */
360 off
= miter
->__sg
->offset
+ miter
->__offset
;
361 len
= miter
->__sg
->length
- miter
->__offset
;
363 miter
->page
= nth_page(sg_page(miter
->__sg
), off
>> PAGE_SHIFT
);
365 miter
->length
= min_t(unsigned int, len
, PAGE_SIZE
- off
);
366 miter
->consumed
= miter
->length
;
368 if (miter
->__flags
& SG_MITER_ATOMIC
)
369 miter
->addr
= kmap_atomic(miter
->page
, KM_BIO_SRC_IRQ
) + off
;
371 miter
->addr
= kmap(miter
->page
) + off
;
375 EXPORT_SYMBOL(sg_miter_next
);
378 * sg_miter_stop - stop mapping iteration
379 * @miter: sg mapping iter to be stopped
382 * Stops mapping iterator @miter. @miter should have been started
383 * started using sg_miter_start(). A stopped iteration can be
384 * resumed by calling sg_miter_next() on it. This is useful when
385 * resources (kmap) need to be released during iteration.
388 * IRQ disabled if the SG_MITER_ATOMIC is set. Don't care otherwise.
390 void sg_miter_stop(struct sg_mapping_iter
*miter
)
392 WARN_ON(miter
->consumed
> miter
->length
);
394 /* drop resources from the last iteration */
396 miter
->__offset
+= miter
->consumed
;
398 if (miter
->__flags
& SG_MITER_TO_SG
)
399 flush_kernel_dcache_page(miter
->page
);
401 if (miter
->__flags
& SG_MITER_ATOMIC
) {
402 WARN_ON(!irqs_disabled());
403 kunmap_atomic(miter
->addr
, KM_BIO_SRC_IRQ
);
413 EXPORT_SYMBOL(sg_miter_stop
);
416 * sg_copy_buffer - Copy data between a linear buffer and an SG list
418 * @nents: Number of SG entries
419 * @buf: Where to copy from
420 * @buflen: The number of bytes to copy
421 * @to_buffer: transfer direction (non zero == from an sg list to a
422 * buffer, 0 == from a buffer to an sg list
424 * Returns the number of copied bytes.
427 static size_t sg_copy_buffer(struct scatterlist
*sgl
, unsigned int nents
,
428 void *buf
, size_t buflen
, int to_buffer
)
430 unsigned int offset
= 0;
431 struct sg_mapping_iter miter
;
433 unsigned int sg_flags
= SG_MITER_ATOMIC
;
436 sg_flags
|= SG_MITER_FROM_SG
;
438 sg_flags
|= SG_MITER_TO_SG
;
440 sg_miter_start(&miter
, sgl
, nents
, sg_flags
);
442 local_irq_save(flags
);
444 while (sg_miter_next(&miter
) && offset
< buflen
) {
447 len
= min(miter
.length
, buflen
- offset
);
450 memcpy(buf
+ offset
, miter
.addr
, len
);
452 memcpy(miter
.addr
, buf
+ offset
, len
);
457 sg_miter_stop(&miter
);
459 local_irq_restore(flags
);
464 * sg_copy_from_buffer - Copy from a linear buffer to an SG list
466 * @nents: Number of SG entries
467 * @buf: Where to copy from
468 * @buflen: The number of bytes to copy
470 * Returns the number of copied bytes.
473 size_t sg_copy_from_buffer(struct scatterlist
*sgl
, unsigned int nents
,
474 void *buf
, size_t buflen
)
476 return sg_copy_buffer(sgl
, nents
, buf
, buflen
, 0);
478 EXPORT_SYMBOL(sg_copy_from_buffer
);
481 * sg_copy_to_buffer - Copy from an SG list to a linear buffer
483 * @nents: Number of SG entries
484 * @buf: Where to copy to
485 * @buflen: The number of bytes to copy
487 * Returns the number of copied bytes.
490 size_t sg_copy_to_buffer(struct scatterlist
*sgl
, unsigned int nents
,
491 void *buf
, size_t buflen
)
493 return sg_copy_buffer(sgl
, nents
, buf
, buflen
, 1);
495 EXPORT_SYMBOL(sg_copy_to_buffer
);