blob: a1cf8cae60e70dd298735f4feab09e55f8141e16 [file] [log] [blame]
Jens Axboe0db92992007-11-30 09:16:50 +01001/*
2 * Copyright (C) 2007 Jens Axboe <jens.axboe@oracle.com>
3 *
4 * Scatterlist handling helpers.
5 *
6 * This source code is licensed under the GNU General Public License,
7 * Version 2. See the file COPYING for more details.
8 */
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -05009#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Jens Axboe0db92992007-11-30 09:16:50 +010011#include <linux/scatterlist.h>
FUJITA Tomonorib1adaf62008-03-18 00:15:03 +090012#include <linux/highmem.h>
Chris Wilsonb94de9b2010-07-28 22:59:02 +010013#include <linux/kmemleak.h>
Jens Axboe0db92992007-11-30 09:16:50 +010014
15/**
16 * sg_next - return the next scatterlist entry in a list
17 * @sg: The current sg entry
18 *
19 * Description:
20 * Usually the next entry will be @sg@ + 1, but if this sg element is part
21 * of a chained scatterlist, it could jump to the start of a new
22 * scatterlist array.
23 *
24 **/
25struct scatterlist *sg_next(struct scatterlist *sg)
26{
27#ifdef CONFIG_DEBUG_SG
28 BUG_ON(sg->sg_magic != SG_MAGIC);
29#endif
30 if (sg_is_last(sg))
31 return NULL;
32
33 sg++;
34 if (unlikely(sg_is_chain(sg)))
35 sg = sg_chain_ptr(sg);
36
37 return sg;
38}
39EXPORT_SYMBOL(sg_next);
40
41/**
Maxim Levitsky2e484612012-09-27 12:45:28 +020042 * sg_nents - return total count of entries in scatterlist
43 * @sg: The scatterlist
44 *
45 * Description:
46 * Allows to know how many entries are in sg, taking into acount
47 * chaining as well
48 *
49 **/
50int sg_nents(struct scatterlist *sg)
51{
Maxim Levitsky232f1b52012-09-28 10:38:15 +020052 int nents;
53 for (nents = 0; sg; sg = sg_next(sg))
Maxim Levitsky2e484612012-09-27 12:45:28 +020054 nents++;
Maxim Levitsky2e484612012-09-27 12:45:28 +020055 return nents;
56}
57EXPORT_SYMBOL(sg_nents);
58
59
60/**
Jens Axboe0db92992007-11-30 09:16:50 +010061 * sg_last - return the last scatterlist entry in a list
62 * @sgl: First entry in the scatterlist
63 * @nents: Number of entries in the scatterlist
64 *
65 * Description:
66 * Should only be used casually, it (currently) scans the entire list
67 * to get the last entry.
68 *
69 * Note that the @sgl@ pointer passed in need not be the first one,
70 * the important bit is that @nents@ denotes the number of entries that
71 * exist from @sgl@.
72 *
73 **/
74struct scatterlist *sg_last(struct scatterlist *sgl, unsigned int nents)
75{
76#ifndef ARCH_HAS_SG_CHAIN
77 struct scatterlist *ret = &sgl[nents - 1];
78#else
79 struct scatterlist *sg, *ret = NULL;
80 unsigned int i;
81
82 for_each_sg(sgl, sg, nents, i)
83 ret = sg;
84
85#endif
86#ifdef CONFIG_DEBUG_SG
87 BUG_ON(sgl[0].sg_magic != SG_MAGIC);
88 BUG_ON(!sg_is_last(ret));
89#endif
90 return ret;
91}
92EXPORT_SYMBOL(sg_last);
93
94/**
95 * sg_init_table - Initialize SG table
96 * @sgl: The SG table
97 * @nents: Number of entries in table
98 *
99 * Notes:
100 * If this is part of a chained sg table, sg_mark_end() should be
101 * used only on the last table part.
102 *
103 **/
104void sg_init_table(struct scatterlist *sgl, unsigned int nents)
105{
106 memset(sgl, 0, sizeof(*sgl) * nents);
107#ifdef CONFIG_DEBUG_SG
108 {
109 unsigned int i;
110 for (i = 0; i < nents; i++)
111 sgl[i].sg_magic = SG_MAGIC;
112 }
113#endif
114 sg_mark_end(&sgl[nents - 1]);
115}
116EXPORT_SYMBOL(sg_init_table);
117
118/**
119 * sg_init_one - Initialize a single entry sg list
120 * @sg: SG entry
121 * @buf: Virtual address for IO
122 * @buflen: IO length
123 *
124 **/
125void sg_init_one(struct scatterlist *sg, const void *buf, unsigned int buflen)
126{
127 sg_init_table(sg, 1);
128 sg_set_buf(sg, buf, buflen);
129}
130EXPORT_SYMBOL(sg_init_one);
131
132/*
133 * The default behaviour of sg_alloc_table() is to use these kmalloc/kfree
134 * helpers.
135 */
136static struct scatterlist *sg_kmalloc(unsigned int nents, gfp_t gfp_mask)
137{
Chris Wilsonb94de9b2010-07-28 22:59:02 +0100138 if (nents == SG_MAX_SINGLE_ALLOC) {
139 /*
140 * Kmemleak doesn't track page allocations as they are not
141 * commonly used (in a raw form) for kernel data structures.
142 * As we chain together a list of pages and then a normal
143 * kmalloc (tracked by kmemleak), in order to for that last
144 * allocation not to become decoupled (and thus a
145 * false-positive) we need to inform kmemleak of all the
146 * intermediate allocations.
147 */
148 void *ptr = (void *) __get_free_page(gfp_mask);
149 kmemleak_alloc(ptr, PAGE_SIZE, 1, gfp_mask);
150 return ptr;
151 } else
Jens Axboe0db92992007-11-30 09:16:50 +0100152 return kmalloc(nents * sizeof(struct scatterlist), gfp_mask);
153}
154
155static void sg_kfree(struct scatterlist *sg, unsigned int nents)
156{
Chris Wilsonb94de9b2010-07-28 22:59:02 +0100157 if (nents == SG_MAX_SINGLE_ALLOC) {
158 kmemleak_free(sg);
Jens Axboe0db92992007-11-30 09:16:50 +0100159 free_page((unsigned long) sg);
Chris Wilsonb94de9b2010-07-28 22:59:02 +0100160 } else
Jens Axboe0db92992007-11-30 09:16:50 +0100161 kfree(sg);
162}
163
164/**
165 * __sg_free_table - Free a previously mapped sg table
166 * @table: The sg table header to use
James Bottomley7cedb1f2008-01-13 14:15:28 -0600167 * @max_ents: The maximum number of entries per single scatterlist
Jens Axboe0db92992007-11-30 09:16:50 +0100168 * @free_fn: Free function
169 *
170 * Description:
James Bottomley7cedb1f2008-01-13 14:15:28 -0600171 * Free an sg table previously allocated and setup with
172 * __sg_alloc_table(). The @max_ents value must be identical to
173 * that previously used with __sg_alloc_table().
Jens Axboe0db92992007-11-30 09:16:50 +0100174 *
175 **/
James Bottomley7cedb1f2008-01-13 14:15:28 -0600176void __sg_free_table(struct sg_table *table, unsigned int max_ents,
177 sg_free_fn *free_fn)
Jens Axboe0db92992007-11-30 09:16:50 +0100178{
179 struct scatterlist *sgl, *next;
180
181 if (unlikely(!table->sgl))
182 return;
183
184 sgl = table->sgl;
185 while (table->orig_nents) {
186 unsigned int alloc_size = table->orig_nents;
187 unsigned int sg_size;
188
189 /*
James Bottomley7cedb1f2008-01-13 14:15:28 -0600190 * If we have more than max_ents segments left,
Jens Axboe0db92992007-11-30 09:16:50 +0100191 * then assign 'next' to the sg table after the current one.
192 * sg_size is then one less than alloc size, since the last
193 * element is the chain pointer.
194 */
James Bottomley7cedb1f2008-01-13 14:15:28 -0600195 if (alloc_size > max_ents) {
196 next = sg_chain_ptr(&sgl[max_ents - 1]);
197 alloc_size = max_ents;
Jens Axboe0db92992007-11-30 09:16:50 +0100198 sg_size = alloc_size - 1;
199 } else {
200 sg_size = alloc_size;
201 next = NULL;
202 }
203
204 table->orig_nents -= sg_size;
205 free_fn(sgl, alloc_size);
206 sgl = next;
207 }
208
209 table->sgl = NULL;
210}
211EXPORT_SYMBOL(__sg_free_table);
212
213/**
214 * sg_free_table - Free a previously allocated sg table
215 * @table: The mapped sg table header
216 *
217 **/
218void sg_free_table(struct sg_table *table)
219{
James Bottomley7cedb1f2008-01-13 14:15:28 -0600220 __sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree);
Jens Axboe0db92992007-11-30 09:16:50 +0100221}
222EXPORT_SYMBOL(sg_free_table);
223
224/**
225 * __sg_alloc_table - Allocate and initialize an sg table with given allocator
226 * @table: The sg table header to use
227 * @nents: Number of entries in sg list
James Bottomley7cedb1f2008-01-13 14:15:28 -0600228 * @max_ents: The maximum number of entries the allocator returns per call
Jens Axboe0db92992007-11-30 09:16:50 +0100229 * @gfp_mask: GFP allocation mask
230 * @alloc_fn: Allocator to use
231 *
James Bottomley7cedb1f2008-01-13 14:15:28 -0600232 * Description:
233 * This function returns a @table @nents long. The allocator is
234 * defined to return scatterlist chunks of maximum size @max_ents.
235 * Thus if @nents is bigger than @max_ents, the scatterlists will be
236 * chained in units of @max_ents.
237 *
Jens Axboe0db92992007-11-30 09:16:50 +0100238 * Notes:
239 * If this function returns non-0 (eg failure), the caller must call
240 * __sg_free_table() to cleanup any leftover allocations.
241 *
242 **/
James Bottomley7cedb1f2008-01-13 14:15:28 -0600243int __sg_alloc_table(struct sg_table *table, unsigned int nents,
244 unsigned int max_ents, gfp_t gfp_mask,
Jens Axboe0db92992007-11-30 09:16:50 +0100245 sg_alloc_fn *alloc_fn)
246{
247 struct scatterlist *sg, *prv;
248 unsigned int left;
249
250#ifndef ARCH_HAS_SG_CHAIN
Nick Bowler6fd59a82012-12-17 16:05:20 -0800251 if (WARN_ON_ONCE(nents > max_ents))
252 return -EINVAL;
Jens Axboe0db92992007-11-30 09:16:50 +0100253#endif
254
255 memset(table, 0, sizeof(*table));
256
257 left = nents;
258 prv = NULL;
259 do {
260 unsigned int sg_size, alloc_size = left;
261
James Bottomley7cedb1f2008-01-13 14:15:28 -0600262 if (alloc_size > max_ents) {
263 alloc_size = max_ents;
Jens Axboe0db92992007-11-30 09:16:50 +0100264 sg_size = alloc_size - 1;
265 } else
266 sg_size = alloc_size;
267
268 left -= sg_size;
269
270 sg = alloc_fn(alloc_size, gfp_mask);
Jeffrey Carlyleedce6822010-08-30 19:55:09 +0200271 if (unlikely(!sg)) {
272 /*
273 * Adjust entry count to reflect that the last
274 * entry of the previous table won't be used for
275 * linkage. Without this, sg_kfree() may get
276 * confused.
277 */
278 if (prv)
279 table->nents = ++table->orig_nents;
280
281 return -ENOMEM;
282 }
Jens Axboe0db92992007-11-30 09:16:50 +0100283
284 sg_init_table(sg, alloc_size);
285 table->nents = table->orig_nents += sg_size;
286
287 /*
288 * If this is the first mapping, assign the sg table header.
289 * If this is not the first mapping, chain previous part.
290 */
291 if (prv)
James Bottomley7cedb1f2008-01-13 14:15:28 -0600292 sg_chain(prv, max_ents, sg);
Jens Axboe0db92992007-11-30 09:16:50 +0100293 else
294 table->sgl = sg;
295
296 /*
297 * If no more entries after this one, mark the end
298 */
299 if (!left)
300 sg_mark_end(&sg[sg_size - 1]);
301
Jens Axboe0db92992007-11-30 09:16:50 +0100302 prv = sg;
303 } while (left);
304
305 return 0;
306}
307EXPORT_SYMBOL(__sg_alloc_table);
308
309/**
310 * sg_alloc_table - Allocate and initialize an sg table
311 * @table: The sg table header to use
312 * @nents: Number of entries in sg list
313 * @gfp_mask: GFP allocation mask
314 *
315 * Description:
316 * Allocate and initialize an sg table. If @nents@ is larger than
317 * SG_MAX_SINGLE_ALLOC a chained sg table will be setup.
318 *
319 **/
320int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
321{
322 int ret;
323
James Bottomley7cedb1f2008-01-13 14:15:28 -0600324 ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
325 gfp_mask, sg_kmalloc);
Jens Axboe0db92992007-11-30 09:16:50 +0100326 if (unlikely(ret))
James Bottomley7cedb1f2008-01-13 14:15:28 -0600327 __sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree);
Jens Axboe0db92992007-11-30 09:16:50 +0100328
329 return ret;
330}
331EXPORT_SYMBOL(sg_alloc_table);
FUJITA Tomonorib1adaf62008-03-18 00:15:03 +0900332
333/**
Tomasz Stanislawskiefc42bc2012-06-18 09:25:01 +0200334 * sg_alloc_table_from_pages - Allocate and initialize an sg table from
335 * an array of pages
336 * @sgt: The sg table header to use
337 * @pages: Pointer to an array of page pointers
338 * @n_pages: Number of pages in the pages array
339 * @offset: Offset from start of the first page to the start of a buffer
340 * @size: Number of valid bytes in the buffer (after offset)
341 * @gfp_mask: GFP allocation mask
342 *
343 * Description:
344 * Allocate and initialize an sg table from a list of pages. Contiguous
345 * ranges of the pages are squashed into a single scatterlist node. A user
346 * may provide an offset at a start and a size of valid data in a buffer
347 * specified by the page array. The returned sg table is released by
348 * sg_free_table.
349 *
350 * Returns:
351 * 0 on success, negative error on failure
352 */
353int sg_alloc_table_from_pages(struct sg_table *sgt,
354 struct page **pages, unsigned int n_pages,
355 unsigned long offset, unsigned long size,
356 gfp_t gfp_mask)
357{
358 unsigned int chunks;
359 unsigned int i;
360 unsigned int cur_page;
361 int ret;
362 struct scatterlist *s;
363
364 /* compute number of contiguous chunks */
365 chunks = 1;
366 for (i = 1; i < n_pages; ++i)
367 if (page_to_pfn(pages[i]) != page_to_pfn(pages[i - 1]) + 1)
368 ++chunks;
369
370 ret = sg_alloc_table(sgt, chunks, gfp_mask);
371 if (unlikely(ret))
372 return ret;
373
374 /* merging chunks and putting them into the scatterlist */
375 cur_page = 0;
376 for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
377 unsigned long chunk_size;
378 unsigned int j;
379
380 /* look for the end of the current chunk */
381 for (j = cur_page + 1; j < n_pages; ++j)
382 if (page_to_pfn(pages[j]) !=
383 page_to_pfn(pages[j - 1]) + 1)
384 break;
385
386 chunk_size = ((j - cur_page) << PAGE_SHIFT) - offset;
387 sg_set_page(s, pages[cur_page], min(size, chunk_size), offset);
388 size -= chunk_size;
389 offset = 0;
390 cur_page = j;
391 }
392
393 return 0;
394}
395EXPORT_SYMBOL(sg_alloc_table_from_pages);
396
Imre Deaka321e912013-02-27 17:02:56 -0800397void __sg_page_iter_start(struct sg_page_iter *piter,
398 struct scatterlist *sglist, unsigned int nents,
399 unsigned long pgoffset)
400{
401 piter->__pg_advance = 0;
402 piter->__nents = nents;
403
Imre Deaka321e912013-02-27 17:02:56 -0800404 piter->sg = sglist;
405 piter->sg_pgoffset = pgoffset;
406}
407EXPORT_SYMBOL(__sg_page_iter_start);
408
409static int sg_page_count(struct scatterlist *sg)
410{
411 return PAGE_ALIGN(sg->offset + sg->length) >> PAGE_SHIFT;
412}
413
414bool __sg_page_iter_next(struct sg_page_iter *piter)
415{
416 if (!piter->__nents || !piter->sg)
417 return false;
418
419 piter->sg_pgoffset += piter->__pg_advance;
420 piter->__pg_advance = 1;
421
422 while (piter->sg_pgoffset >= sg_page_count(piter->sg)) {
423 piter->sg_pgoffset -= sg_page_count(piter->sg);
424 piter->sg = sg_next(piter->sg);
425 if (!--piter->__nents || !piter->sg)
426 return false;
427 }
Imre Deaka321e912013-02-27 17:02:56 -0800428
429 return true;
430}
431EXPORT_SYMBOL(__sg_page_iter_next);
432
Tomasz Stanislawskiefc42bc2012-06-18 09:25:01 +0200433/**
Tejun Heo137d3ed2008-07-19 23:03:35 +0900434 * sg_miter_start - start mapping iteration over a sg list
435 * @miter: sg mapping iter to be started
436 * @sgl: sg list to iterate over
437 * @nents: number of sg entries
438 *
439 * Description:
440 * Starts mapping iterator @miter.
441 *
442 * Context:
443 * Don't care.
444 */
445void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
446 unsigned int nents, unsigned int flags)
447{
448 memset(miter, 0, sizeof(struct sg_mapping_iter));
449
Imre Deak4225fc82013-02-27 17:02:57 -0800450 __sg_page_iter_start(&miter->piter, sgl, nents, 0);
Sebastian Andrzej Siewior6de7e3562009-06-18 10:19:12 +0200451 WARN_ON(!(flags & (SG_MITER_TO_SG | SG_MITER_FROM_SG)));
Tejun Heo137d3ed2008-07-19 23:03:35 +0900452 miter->__flags = flags;
453}
454EXPORT_SYMBOL(sg_miter_start);
455
456/**
457 * sg_miter_next - proceed mapping iterator to the next mapping
458 * @miter: sg mapping iter to proceed
459 *
460 * Description:
Tejun Heo8290e2d2012-10-04 17:13:28 -0700461 * Proceeds @miter to the next mapping. @miter should have been started
462 * using sg_miter_start(). On successful return, @miter->page,
463 * @miter->addr and @miter->length point to the current mapping.
Tejun Heo137d3ed2008-07-19 23:03:35 +0900464 *
465 * Context:
Tejun Heo8290e2d2012-10-04 17:13:28 -0700466 * Preemption disabled if SG_MITER_ATOMIC. Preemption must stay disabled
467 * till @miter is stopped. May sleep if !SG_MITER_ATOMIC.
Tejun Heo137d3ed2008-07-19 23:03:35 +0900468 *
469 * Returns:
470 * true if @miter contains the next mapping. false if end of sg
471 * list is reached.
472 */
473bool sg_miter_next(struct sg_mapping_iter *miter)
474{
Tejun Heo137d3ed2008-07-19 23:03:35 +0900475 sg_miter_stop(miter);
476
Imre Deak4225fc82013-02-27 17:02:57 -0800477 /*
478 * Get to the next page if necessary.
479 * __remaining, __offset is adjusted by sg_miter_stop
480 */
481 if (!miter->__remaining) {
482 struct scatterlist *sg;
483 unsigned long pgoffset;
484
485 if (!__sg_page_iter_next(&miter->piter))
Tejun Heo23c560a2009-04-15 22:10:23 +0900486 return false;
Imre Deak4225fc82013-02-27 17:02:57 -0800487
488 sg = miter->piter.sg;
489 pgoffset = miter->piter.sg_pgoffset;
490
491 miter->__offset = pgoffset ? 0 : sg->offset;
492 miter->__remaining = sg->offset + sg->length -
493 (pgoffset << PAGE_SHIFT) - miter->__offset;
494 miter->__remaining = min_t(unsigned long, miter->__remaining,
495 PAGE_SIZE - miter->__offset);
Tejun Heo137d3ed2008-07-19 23:03:35 +0900496 }
Imre Deak2db76d72013-03-26 15:14:18 +0200497 miter->page = sg_page_iter_page(&miter->piter);
Imre Deak4225fc82013-02-27 17:02:57 -0800498 miter->consumed = miter->length = miter->__remaining;
Tejun Heo137d3ed2008-07-19 23:03:35 +0900499
500 if (miter->__flags & SG_MITER_ATOMIC)
Imre Deak4225fc82013-02-27 17:02:57 -0800501 miter->addr = kmap_atomic(miter->page) + miter->__offset;
Tejun Heo137d3ed2008-07-19 23:03:35 +0900502 else
Imre Deak4225fc82013-02-27 17:02:57 -0800503 miter->addr = kmap(miter->page) + miter->__offset;
Tejun Heo137d3ed2008-07-19 23:03:35 +0900504
505 return true;
506}
507EXPORT_SYMBOL(sg_miter_next);
508
509/**
510 * sg_miter_stop - stop mapping iteration
511 * @miter: sg mapping iter to be stopped
512 *
513 * Description:
514 * Stops mapping iterator @miter. @miter should have been started
515 * started using sg_miter_start(). A stopped iteration can be
516 * resumed by calling sg_miter_next() on it. This is useful when
517 * resources (kmap) need to be released during iteration.
518 *
519 * Context:
Tejun Heo8290e2d2012-10-04 17:13:28 -0700520 * Preemption disabled if the SG_MITER_ATOMIC is set. Don't care
521 * otherwise.
Tejun Heo137d3ed2008-07-19 23:03:35 +0900522 */
523void sg_miter_stop(struct sg_mapping_iter *miter)
524{
525 WARN_ON(miter->consumed > miter->length);
526
527 /* drop resources from the last iteration */
528 if (miter->addr) {
529 miter->__offset += miter->consumed;
Imre Deak4225fc82013-02-27 17:02:57 -0800530 miter->__remaining -= miter->consumed;
Tejun Heo137d3ed2008-07-19 23:03:35 +0900531
Sebastian Andrzej Siewior6de7e3562009-06-18 10:19:12 +0200532 if (miter->__flags & SG_MITER_TO_SG)
533 flush_kernel_dcache_page(miter->page);
534
Tejun Heo137d3ed2008-07-19 23:03:35 +0900535 if (miter->__flags & SG_MITER_ATOMIC) {
Tejun Heo8290e2d2012-10-04 17:13:28 -0700536 WARN_ON_ONCE(preemptible());
Cong Wangc3eede82011-11-25 23:14:39 +0800537 kunmap_atomic(miter->addr);
Tejun Heo137d3ed2008-07-19 23:03:35 +0900538 } else
Arjan van de Venf652c522008-11-19 15:36:19 -0800539 kunmap(miter->page);
Tejun Heo137d3ed2008-07-19 23:03:35 +0900540
541 miter->page = NULL;
542 miter->addr = NULL;
543 miter->length = 0;
544 miter->consumed = 0;
545 }
546}
547EXPORT_SYMBOL(sg_miter_stop);
548
549/**
FUJITA Tomonorib1adaf62008-03-18 00:15:03 +0900550 * sg_copy_buffer - Copy data between a linear buffer and an SG list
551 * @sgl: The SG list
552 * @nents: Number of SG entries
553 * @buf: Where to copy from
554 * @buflen: The number of bytes to copy
555 * @to_buffer: transfer direction (non zero == from an sg list to a
556 * buffer, 0 == from a buffer to an sg list
557 *
558 * Returns the number of copied bytes.
559 *
560 **/
561static size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents,
562 void *buf, size_t buflen, int to_buffer)
563{
Tejun Heo137d3ed2008-07-19 23:03:35 +0900564 unsigned int offset = 0;
565 struct sg_mapping_iter miter;
FUJITA Tomonori50bed2e2008-09-11 18:35:39 +0200566 unsigned long flags;
Sebastian Andrzej Siewior6de7e3562009-06-18 10:19:12 +0200567 unsigned int sg_flags = SG_MITER_ATOMIC;
FUJITA Tomonorib1adaf62008-03-18 00:15:03 +0900568
Sebastian Andrzej Siewior6de7e3562009-06-18 10:19:12 +0200569 if (to_buffer)
570 sg_flags |= SG_MITER_FROM_SG;
571 else
572 sg_flags |= SG_MITER_TO_SG;
573
574 sg_miter_start(&miter, sgl, nents, sg_flags);
FUJITA Tomonorib1adaf62008-03-18 00:15:03 +0900575
FUJITA Tomonori50bed2e2008-09-11 18:35:39 +0200576 local_irq_save(flags);
577
Tejun Heo137d3ed2008-07-19 23:03:35 +0900578 while (sg_miter_next(&miter) && offset < buflen) {
579 unsigned int len;
FUJITA Tomonorib1adaf62008-03-18 00:15:03 +0900580
Tejun Heo137d3ed2008-07-19 23:03:35 +0900581 len = min(miter.length, buflen - offset);
FUJITA Tomonorib1adaf62008-03-18 00:15:03 +0900582
Tejun Heo137d3ed2008-07-19 23:03:35 +0900583 if (to_buffer)
584 memcpy(buf + offset, miter.addr, len);
Sebastian Andrzej Siewior6de7e3562009-06-18 10:19:12 +0200585 else
Tejun Heo137d3ed2008-07-19 23:03:35 +0900586 memcpy(miter.addr, buf + offset, len);
FUJITA Tomonorib1adaf62008-03-18 00:15:03 +0900587
Tejun Heo137d3ed2008-07-19 23:03:35 +0900588 offset += len;
FUJITA Tomonorib1adaf62008-03-18 00:15:03 +0900589 }
590
Tejun Heo137d3ed2008-07-19 23:03:35 +0900591 sg_miter_stop(&miter);
592
FUJITA Tomonori50bed2e2008-09-11 18:35:39 +0200593 local_irq_restore(flags);
Tejun Heo137d3ed2008-07-19 23:03:35 +0900594 return offset;
FUJITA Tomonorib1adaf62008-03-18 00:15:03 +0900595}
596
597/**
598 * sg_copy_from_buffer - Copy from a linear buffer to an SG list
599 * @sgl: The SG list
600 * @nents: Number of SG entries
601 * @buf: Where to copy from
602 * @buflen: The number of bytes to copy
603 *
604 * Returns the number of copied bytes.
605 *
606 **/
607size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
608 void *buf, size_t buflen)
609{
610 return sg_copy_buffer(sgl, nents, buf, buflen, 0);
611}
612EXPORT_SYMBOL(sg_copy_from_buffer);
613
614/**
615 * sg_copy_to_buffer - Copy from an SG list to a linear buffer
616 * @sgl: The SG list
617 * @nents: Number of SG entries
618 * @buf: Where to copy to
619 * @buflen: The number of bytes to copy
620 *
621 * Returns the number of copied bytes.
622 *
623 **/
624size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
625 void *buf, size_t buflen)
626{
627 return sg_copy_buffer(sgl, nents, buf, buflen, 1);
628}
629EXPORT_SYMBOL(sg_copy_to_buffer);