Merge tag 'dm-3.6-changes' of git://git.kernel.org/pub/scm/linux/kernel/git/agk/linux-dm
[pandora-kernel.git] / lib / scatterlist.c
1 /*
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  */
9 #include <linux/export.h>
10 #include <linux/slab.h>
11 #include <linux/scatterlist.h>
12 #include <linux/highmem.h>
13 #include <linux/kmemleak.h>
14
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  **/
25 struct 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 }
39 EXPORT_SYMBOL(sg_next);
40
41 /**
42  * sg_last - return the last scatterlist entry in a list
43  * @sgl:        First entry in the scatterlist
44  * @nents:      Number of entries in the scatterlist
45  *
46  * Description:
47  *   Should only be used casually, it (currently) scans the entire list
48  *   to get the last entry.
49  *
50  *   Note that the @sgl@ pointer passed in need not be the first one,
51  *   the important bit is that @nents@ denotes the number of entries that
52  *   exist from @sgl@.
53  *
54  **/
55 struct scatterlist *sg_last(struct scatterlist *sgl, unsigned int nents)
56 {
57 #ifndef ARCH_HAS_SG_CHAIN
58         struct scatterlist *ret = &sgl[nents - 1];
59 #else
60         struct scatterlist *sg, *ret = NULL;
61         unsigned int i;
62
63         for_each_sg(sgl, sg, nents, i)
64                 ret = sg;
65
66 #endif
67 #ifdef CONFIG_DEBUG_SG
68         BUG_ON(sgl[0].sg_magic != SG_MAGIC);
69         BUG_ON(!sg_is_last(ret));
70 #endif
71         return ret;
72 }
73 EXPORT_SYMBOL(sg_last);
74
75 /**
76  * sg_init_table - Initialize SG table
77  * @sgl:           The SG table
78  * @nents:         Number of entries in table
79  *
80  * Notes:
81  *   If this is part of a chained sg table, sg_mark_end() should be
82  *   used only on the last table part.
83  *
84  **/
85 void sg_init_table(struct scatterlist *sgl, unsigned int nents)
86 {
87         memset(sgl, 0, sizeof(*sgl) * nents);
88 #ifdef CONFIG_DEBUG_SG
89         {
90                 unsigned int i;
91                 for (i = 0; i < nents; i++)
92                         sgl[i].sg_magic = SG_MAGIC;
93         }
94 #endif
95         sg_mark_end(&sgl[nents - 1]);
96 }
97 EXPORT_SYMBOL(sg_init_table);
98
99 /**
100  * sg_init_one - Initialize a single entry sg list
101  * @sg:          SG entry
102  * @buf:         Virtual address for IO
103  * @buflen:      IO length
104  *
105  **/
106 void sg_init_one(struct scatterlist *sg, const void *buf, unsigned int buflen)
107 {
108         sg_init_table(sg, 1);
109         sg_set_buf(sg, buf, buflen);
110 }
111 EXPORT_SYMBOL(sg_init_one);
112
113 /*
114  * The default behaviour of sg_alloc_table() is to use these kmalloc/kfree
115  * helpers.
116  */
117 static struct scatterlist *sg_kmalloc(unsigned int nents, gfp_t gfp_mask)
118 {
119         if (nents == SG_MAX_SINGLE_ALLOC) {
120                 /*
121                  * Kmemleak doesn't track page allocations as they are not
122                  * commonly used (in a raw form) for kernel data structures.
123                  * As we chain together a list of pages and then a normal
124                  * kmalloc (tracked by kmemleak), in order to for that last
125                  * allocation not to become decoupled (and thus a
126                  * false-positive) we need to inform kmemleak of all the
127                  * intermediate allocations.
128                  */
129                 void *ptr = (void *) __get_free_page(gfp_mask);
130                 kmemleak_alloc(ptr, PAGE_SIZE, 1, gfp_mask);
131                 return ptr;
132         } else
133                 return kmalloc(nents * sizeof(struct scatterlist), gfp_mask);
134 }
135
136 static void sg_kfree(struct scatterlist *sg, unsigned int nents)
137 {
138         if (nents == SG_MAX_SINGLE_ALLOC) {
139                 kmemleak_free(sg);
140                 free_page((unsigned long) sg);
141         } else
142                 kfree(sg);
143 }
144
145 /**
146  * __sg_free_table - Free a previously mapped sg table
147  * @table:      The sg table header to use
148  * @max_ents:   The maximum number of entries per single scatterlist
149  * @free_fn:    Free function
150  *
151  *  Description:
152  *    Free an sg table previously allocated and setup with
153  *    __sg_alloc_table().  The @max_ents value must be identical to
154  *    that previously used with __sg_alloc_table().
155  *
156  **/
157 void __sg_free_table(struct sg_table *table, unsigned int max_ents,
158                      sg_free_fn *free_fn)
159 {
160         struct scatterlist *sgl, *next;
161
162         if (unlikely(!table->sgl))
163                 return;
164
165         sgl = table->sgl;
166         while (table->orig_nents) {
167                 unsigned int alloc_size = table->orig_nents;
168                 unsigned int sg_size;
169
170                 /*
171                  * If we have more than max_ents segments left,
172                  * then assign 'next' to the sg table after the current one.
173                  * sg_size is then one less than alloc size, since the last
174                  * element is the chain pointer.
175                  */
176                 if (alloc_size > max_ents) {
177                         next = sg_chain_ptr(&sgl[max_ents - 1]);
178                         alloc_size = max_ents;
179                         sg_size = alloc_size - 1;
180                 } else {
181                         sg_size = alloc_size;
182                         next = NULL;
183                 }
184
185                 table->orig_nents -= sg_size;
186                 free_fn(sgl, alloc_size);
187                 sgl = next;
188         }
189
190         table->sgl = NULL;
191 }
192 EXPORT_SYMBOL(__sg_free_table);
193
194 /**
195  * sg_free_table - Free a previously allocated sg table
196  * @table:      The mapped sg table header
197  *
198  **/
199 void sg_free_table(struct sg_table *table)
200 {
201         __sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree);
202 }
203 EXPORT_SYMBOL(sg_free_table);
204
205 /**
206  * __sg_alloc_table - Allocate and initialize an sg table with given allocator
207  * @table:      The sg table header to use
208  * @nents:      Number of entries in sg list
209  * @max_ents:   The maximum number of entries the allocator returns per call
210  * @gfp_mask:   GFP allocation mask
211  * @alloc_fn:   Allocator to use
212  *
213  * Description:
214  *   This function returns a @table @nents long. The allocator is
215  *   defined to return scatterlist chunks of maximum size @max_ents.
216  *   Thus if @nents is bigger than @max_ents, the scatterlists will be
217  *   chained in units of @max_ents.
218  *
219  * Notes:
220  *   If this function returns non-0 (eg failure), the caller must call
221  *   __sg_free_table() to cleanup any leftover allocations.
222  *
223  **/
224 int __sg_alloc_table(struct sg_table *table, unsigned int nents,
225                      unsigned int max_ents, gfp_t gfp_mask,
226                      sg_alloc_fn *alloc_fn)
227 {
228         struct scatterlist *sg, *prv;
229         unsigned int left;
230
231 #ifndef ARCH_HAS_SG_CHAIN
232         BUG_ON(nents > max_ents);
233 #endif
234
235         memset(table, 0, sizeof(*table));
236
237         left = nents;
238         prv = NULL;
239         do {
240                 unsigned int sg_size, alloc_size = left;
241
242                 if (alloc_size > max_ents) {
243                         alloc_size = max_ents;
244                         sg_size = alloc_size - 1;
245                 } else
246                         sg_size = alloc_size;
247
248                 left -= sg_size;
249
250                 sg = alloc_fn(alloc_size, gfp_mask);
251                 if (unlikely(!sg)) {
252                         /*
253                          * Adjust entry count to reflect that the last
254                          * entry of the previous table won't be used for
255                          * linkage.  Without this, sg_kfree() may get
256                          * confused.
257                          */
258                         if (prv)
259                                 table->nents = ++table->orig_nents;
260
261                         return -ENOMEM;
262                 }
263
264                 sg_init_table(sg, alloc_size);
265                 table->nents = table->orig_nents += sg_size;
266
267                 /*
268                  * If this is the first mapping, assign the sg table header.
269                  * If this is not the first mapping, chain previous part.
270                  */
271                 if (prv)
272                         sg_chain(prv, max_ents, sg);
273                 else
274                         table->sgl = sg;
275
276                 /*
277                  * If no more entries after this one, mark the end
278                  */
279                 if (!left)
280                         sg_mark_end(&sg[sg_size - 1]);
281
282                 /*
283                  * only really needed for mempool backed sg allocations (like
284                  * SCSI), a possible improvement here would be to pass the
285                  * table pointer into the allocator and let that clear these
286                  * flags
287                  */
288                 gfp_mask &= ~__GFP_WAIT;
289                 gfp_mask |= __GFP_HIGH;
290                 prv = sg;
291         } while (left);
292
293         return 0;
294 }
295 EXPORT_SYMBOL(__sg_alloc_table);
296
297 /**
298  * sg_alloc_table - Allocate and initialize an sg table
299  * @table:      The sg table header to use
300  * @nents:      Number of entries in sg list
301  * @gfp_mask:   GFP allocation mask
302  *
303  *  Description:
304  *    Allocate and initialize an sg table. If @nents@ is larger than
305  *    SG_MAX_SINGLE_ALLOC a chained sg table will be setup.
306  *
307  **/
308 int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
309 {
310         int ret;
311
312         ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
313                                gfp_mask, sg_kmalloc);
314         if (unlikely(ret))
315                 __sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree);
316
317         return ret;
318 }
319 EXPORT_SYMBOL(sg_alloc_table);
320
321 /**
322  * sg_alloc_table_from_pages - Allocate and initialize an sg table from
323  *                             an array of pages
324  * @sgt:        The sg table header to use
325  * @pages:      Pointer to an array of page pointers
326  * @n_pages:    Number of pages in the pages array
327  * @offset:     Offset from start of the first page to the start of a buffer
328  * @size:       Number of valid bytes in the buffer (after offset)
329  * @gfp_mask:   GFP allocation mask
330  *
331  *  Description:
332  *    Allocate and initialize an sg table from a list of pages. Contiguous
333  *    ranges of the pages are squashed into a single scatterlist node. A user
334  *    may provide an offset at a start and a size of valid data in a buffer
335  *    specified by the page array. The returned sg table is released by
336  *    sg_free_table.
337  *
338  * Returns:
339  *   0 on success, negative error on failure
340  */
341 int sg_alloc_table_from_pages(struct sg_table *sgt,
342         struct page **pages, unsigned int n_pages,
343         unsigned long offset, unsigned long size,
344         gfp_t gfp_mask)
345 {
346         unsigned int chunks;
347         unsigned int i;
348         unsigned int cur_page;
349         int ret;
350         struct scatterlist *s;
351
352         /* compute number of contiguous chunks */
353         chunks = 1;
354         for (i = 1; i < n_pages; ++i)
355                 if (page_to_pfn(pages[i]) != page_to_pfn(pages[i - 1]) + 1)
356                         ++chunks;
357
358         ret = sg_alloc_table(sgt, chunks, gfp_mask);
359         if (unlikely(ret))
360                 return ret;
361
362         /* merging chunks and putting them into the scatterlist */
363         cur_page = 0;
364         for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
365                 unsigned long chunk_size;
366                 unsigned int j;
367
368                 /* look for the end of the current chunk */
369                 for (j = cur_page + 1; j < n_pages; ++j)
370                         if (page_to_pfn(pages[j]) !=
371                             page_to_pfn(pages[j - 1]) + 1)
372                                 break;
373
374                 chunk_size = ((j - cur_page) << PAGE_SHIFT) - offset;
375                 sg_set_page(s, pages[cur_page], min(size, chunk_size), offset);
376                 size -= chunk_size;
377                 offset = 0;
378                 cur_page = j;
379         }
380
381         return 0;
382 }
383 EXPORT_SYMBOL(sg_alloc_table_from_pages);
384
385 /**
386  * sg_miter_start - start mapping iteration over a sg list
387  * @miter: sg mapping iter to be started
388  * @sgl: sg list to iterate over
389  * @nents: number of sg entries
390  *
391  * Description:
392  *   Starts mapping iterator @miter.
393  *
394  * Context:
395  *   Don't care.
396  */
397 void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
398                     unsigned int nents, unsigned int flags)
399 {
400         memset(miter, 0, sizeof(struct sg_mapping_iter));
401
402         miter->__sg = sgl;
403         miter->__nents = nents;
404         miter->__offset = 0;
405         WARN_ON(!(flags & (SG_MITER_TO_SG | SG_MITER_FROM_SG)));
406         miter->__flags = flags;
407 }
408 EXPORT_SYMBOL(sg_miter_start);
409
410 /**
411  * sg_miter_next - proceed mapping iterator to the next mapping
412  * @miter: sg mapping iter to proceed
413  *
414  * Description:
415  *   Proceeds @miter@ to the next mapping.  @miter@ should have been
416  *   started using sg_miter_start().  On successful return,
417  *   @miter@->page, @miter@->addr and @miter@->length point to the
418  *   current mapping.
419  *
420  * Context:
421  *   IRQ disabled if SG_MITER_ATOMIC.  IRQ must stay disabled till
422  *   @miter@ is stopped.  May sleep if !SG_MITER_ATOMIC.
423  *
424  * Returns:
425  *   true if @miter contains the next mapping.  false if end of sg
426  *   list is reached.
427  */
428 bool sg_miter_next(struct sg_mapping_iter *miter)
429 {
430         unsigned int off, len;
431
432         /* check for end and drop resources from the last iteration */
433         if (!miter->__nents)
434                 return false;
435
436         sg_miter_stop(miter);
437
438         /* get to the next sg if necessary.  __offset is adjusted by stop */
439         while (miter->__offset == miter->__sg->length) {
440                 if (--miter->__nents) {
441                         miter->__sg = sg_next(miter->__sg);
442                         miter->__offset = 0;
443                 } else
444                         return false;
445         }
446
447         /* map the next page */
448         off = miter->__sg->offset + miter->__offset;
449         len = miter->__sg->length - miter->__offset;
450
451         miter->page = nth_page(sg_page(miter->__sg), off >> PAGE_SHIFT);
452         off &= ~PAGE_MASK;
453         miter->length = min_t(unsigned int, len, PAGE_SIZE - off);
454         miter->consumed = miter->length;
455
456         if (miter->__flags & SG_MITER_ATOMIC)
457                 miter->addr = kmap_atomic(miter->page) + off;
458         else
459                 miter->addr = kmap(miter->page) + off;
460
461         return true;
462 }
463 EXPORT_SYMBOL(sg_miter_next);
464
465 /**
466  * sg_miter_stop - stop mapping iteration
467  * @miter: sg mapping iter to be stopped
468  *
469  * Description:
470  *   Stops mapping iterator @miter.  @miter should have been started
471  *   started using sg_miter_start().  A stopped iteration can be
472  *   resumed by calling sg_miter_next() on it.  This is useful when
473  *   resources (kmap) need to be released during iteration.
474  *
475  * Context:
476  *   IRQ disabled if the SG_MITER_ATOMIC is set.  Don't care otherwise.
477  */
478 void sg_miter_stop(struct sg_mapping_iter *miter)
479 {
480         WARN_ON(miter->consumed > miter->length);
481
482         /* drop resources from the last iteration */
483         if (miter->addr) {
484                 miter->__offset += miter->consumed;
485
486                 if (miter->__flags & SG_MITER_TO_SG)
487                         flush_kernel_dcache_page(miter->page);
488
489                 if (miter->__flags & SG_MITER_ATOMIC) {
490                         WARN_ON(!irqs_disabled());
491                         kunmap_atomic(miter->addr);
492                 } else
493                         kunmap(miter->page);
494
495                 miter->page = NULL;
496                 miter->addr = NULL;
497                 miter->length = 0;
498                 miter->consumed = 0;
499         }
500 }
501 EXPORT_SYMBOL(sg_miter_stop);
502
503 /**
504  * sg_copy_buffer - Copy data between a linear buffer and an SG list
505  * @sgl:                 The SG list
506  * @nents:               Number of SG entries
507  * @buf:                 Where to copy from
508  * @buflen:              The number of bytes to copy
509  * @to_buffer:           transfer direction (non zero == from an sg list to a
510  *                       buffer, 0 == from a buffer to an sg list
511  *
512  * Returns the number of copied bytes.
513  *
514  **/
515 static size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents,
516                              void *buf, size_t buflen, int to_buffer)
517 {
518         unsigned int offset = 0;
519         struct sg_mapping_iter miter;
520         unsigned long flags;
521         unsigned int sg_flags = SG_MITER_ATOMIC;
522
523         if (to_buffer)
524                 sg_flags |= SG_MITER_FROM_SG;
525         else
526                 sg_flags |= SG_MITER_TO_SG;
527
528         sg_miter_start(&miter, sgl, nents, sg_flags);
529
530         local_irq_save(flags);
531
532         while (sg_miter_next(&miter) && offset < buflen) {
533                 unsigned int len;
534
535                 len = min(miter.length, buflen - offset);
536
537                 if (to_buffer)
538                         memcpy(buf + offset, miter.addr, len);
539                 else
540                         memcpy(miter.addr, buf + offset, len);
541
542                 offset += len;
543         }
544
545         sg_miter_stop(&miter);
546
547         local_irq_restore(flags);
548         return offset;
549 }
550
551 /**
552  * sg_copy_from_buffer - Copy from a linear buffer to an SG list
553  * @sgl:                 The SG list
554  * @nents:               Number of SG entries
555  * @buf:                 Where to copy from
556  * @buflen:              The number of bytes to copy
557  *
558  * Returns the number of copied bytes.
559  *
560  **/
561 size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
562                            void *buf, size_t buflen)
563 {
564         return sg_copy_buffer(sgl, nents, buf, buflen, 0);
565 }
566 EXPORT_SYMBOL(sg_copy_from_buffer);
567
568 /**
569  * sg_copy_to_buffer - Copy from an SG list to a linear buffer
570  * @sgl:                 The SG list
571  * @nents:               Number of SG entries
572  * @buf:                 Where to copy to
573  * @buflen:              The number of bytes to copy
574  *
575  * Returns the number of copied bytes.
576  *
577  **/
578 size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
579                          void *buf, size_t buflen)
580 {
581         return sg_copy_buffer(sgl, nents, buf, buflen, 1);
582 }
583 EXPORT_SYMBOL(sg_copy_to_buffer);