10db9299fSJens Axboe /* 20db9299fSJens Axboe * Copyright (C) 2007 Jens Axboe <jens.axboe@oracle.com> 30db9299fSJens Axboe * 40db9299fSJens Axboe * Scatterlist handling helpers. 50db9299fSJens Axboe * 60db9299fSJens Axboe * This source code is licensed under the GNU General Public License, 70db9299fSJens Axboe * Version 2. See the file COPYING for more details. 80db9299fSJens Axboe */ 90db9299fSJens Axboe #include <linux/module.h> 105a0e3ad6STejun Heo #include <linux/slab.h> 110db9299fSJens Axboe #include <linux/scatterlist.h> 12b1adaf65SFUJITA Tomonori #include <linux/highmem.h> 13b94de9bbSChris Wilson #include <linux/kmemleak.h> 140db9299fSJens Axboe 150db9299fSJens Axboe /** 160db9299fSJens Axboe * sg_next - return the next scatterlist entry in a list 170db9299fSJens Axboe * @sg: The current sg entry 180db9299fSJens Axboe * 190db9299fSJens Axboe * Description: 200db9299fSJens Axboe * Usually the next entry will be @sg@ + 1, but if this sg element is part 210db9299fSJens Axboe * of a chained scatterlist, it could jump to the start of a new 220db9299fSJens Axboe * scatterlist array. 230db9299fSJens Axboe * 240db9299fSJens Axboe **/ 250db9299fSJens Axboe struct scatterlist *sg_next(struct scatterlist *sg) 260db9299fSJens Axboe { 270db9299fSJens Axboe #ifdef CONFIG_DEBUG_SG 280db9299fSJens Axboe BUG_ON(sg->sg_magic != SG_MAGIC); 290db9299fSJens Axboe #endif 300db9299fSJens Axboe if (sg_is_last(sg)) 310db9299fSJens Axboe return NULL; 320db9299fSJens Axboe 330db9299fSJens Axboe sg++; 340db9299fSJens Axboe if (unlikely(sg_is_chain(sg))) 350db9299fSJens Axboe sg = sg_chain_ptr(sg); 360db9299fSJens Axboe 370db9299fSJens Axboe return sg; 380db9299fSJens Axboe } 390db9299fSJens Axboe EXPORT_SYMBOL(sg_next); 400db9299fSJens Axboe 410db9299fSJens Axboe /** 420db9299fSJens Axboe * sg_last - return the last scatterlist entry in a list 430db9299fSJens Axboe * @sgl: First entry in the scatterlist 440db9299fSJens Axboe * @nents: Number of entries in the scatterlist 450db9299fSJens Axboe * 460db9299fSJens Axboe * Description: 470db9299fSJens Axboe * Should only be used casually, it (currently) scans the entire list 480db9299fSJens Axboe * to get the last entry. 490db9299fSJens Axboe * 500db9299fSJens Axboe * Note that the @sgl@ pointer passed in need not be the first one, 510db9299fSJens Axboe * the important bit is that @nents@ denotes the number of entries that 520db9299fSJens Axboe * exist from @sgl@. 530db9299fSJens Axboe * 540db9299fSJens Axboe **/ 550db9299fSJens Axboe struct scatterlist *sg_last(struct scatterlist *sgl, unsigned int nents) 560db9299fSJens Axboe { 570db9299fSJens Axboe #ifndef ARCH_HAS_SG_CHAIN 580db9299fSJens Axboe struct scatterlist *ret = &sgl[nents - 1]; 590db9299fSJens Axboe #else 600db9299fSJens Axboe struct scatterlist *sg, *ret = NULL; 610db9299fSJens Axboe unsigned int i; 620db9299fSJens Axboe 630db9299fSJens Axboe for_each_sg(sgl, sg, nents, i) 640db9299fSJens Axboe ret = sg; 650db9299fSJens Axboe 660db9299fSJens Axboe #endif 670db9299fSJens Axboe #ifdef CONFIG_DEBUG_SG 680db9299fSJens Axboe BUG_ON(sgl[0].sg_magic != SG_MAGIC); 690db9299fSJens Axboe BUG_ON(!sg_is_last(ret)); 700db9299fSJens Axboe #endif 710db9299fSJens Axboe return ret; 720db9299fSJens Axboe } 730db9299fSJens Axboe EXPORT_SYMBOL(sg_last); 740db9299fSJens Axboe 750db9299fSJens Axboe /** 760db9299fSJens Axboe * sg_init_table - Initialize SG table 770db9299fSJens Axboe * @sgl: The SG table 780db9299fSJens Axboe * @nents: Number of entries in table 790db9299fSJens Axboe * 800db9299fSJens Axboe * Notes: 810db9299fSJens Axboe * If this is part of a chained sg table, sg_mark_end() should be 820db9299fSJens Axboe * used only on the last table part. 830db9299fSJens Axboe * 840db9299fSJens Axboe **/ 850db9299fSJens Axboe void sg_init_table(struct scatterlist *sgl, unsigned int nents) 860db9299fSJens Axboe { 870db9299fSJens Axboe memset(sgl, 0, sizeof(*sgl) * nents); 880db9299fSJens Axboe #ifdef CONFIG_DEBUG_SG 890db9299fSJens Axboe { 900db9299fSJens Axboe unsigned int i; 910db9299fSJens Axboe for (i = 0; i < nents; i++) 920db9299fSJens Axboe sgl[i].sg_magic = SG_MAGIC; 930db9299fSJens Axboe } 940db9299fSJens Axboe #endif 950db9299fSJens Axboe sg_mark_end(&sgl[nents - 1]); 960db9299fSJens Axboe } 970db9299fSJens Axboe EXPORT_SYMBOL(sg_init_table); 980db9299fSJens Axboe 990db9299fSJens Axboe /** 1000db9299fSJens Axboe * sg_init_one - Initialize a single entry sg list 1010db9299fSJens Axboe * @sg: SG entry 1020db9299fSJens Axboe * @buf: Virtual address for IO 1030db9299fSJens Axboe * @buflen: IO length 1040db9299fSJens Axboe * 1050db9299fSJens Axboe **/ 1060db9299fSJens Axboe void sg_init_one(struct scatterlist *sg, const void *buf, unsigned int buflen) 1070db9299fSJens Axboe { 1080db9299fSJens Axboe sg_init_table(sg, 1); 1090db9299fSJens Axboe sg_set_buf(sg, buf, buflen); 1100db9299fSJens Axboe } 1110db9299fSJens Axboe EXPORT_SYMBOL(sg_init_one); 1120db9299fSJens Axboe 1130db9299fSJens Axboe /* 1140db9299fSJens Axboe * The default behaviour of sg_alloc_table() is to use these kmalloc/kfree 1150db9299fSJens Axboe * helpers. 1160db9299fSJens Axboe */ 1170db9299fSJens Axboe static struct scatterlist *sg_kmalloc(unsigned int nents, gfp_t gfp_mask) 1180db9299fSJens Axboe { 119b94de9bbSChris Wilson if (nents == SG_MAX_SINGLE_ALLOC) { 120b94de9bbSChris Wilson /* 121b94de9bbSChris Wilson * Kmemleak doesn't track page allocations as they are not 122b94de9bbSChris Wilson * commonly used (in a raw form) for kernel data structures. 123b94de9bbSChris Wilson * As we chain together a list of pages and then a normal 124b94de9bbSChris Wilson * kmalloc (tracked by kmemleak), in order to for that last 125b94de9bbSChris Wilson * allocation not to become decoupled (and thus a 126b94de9bbSChris Wilson * false-positive) we need to inform kmemleak of all the 127b94de9bbSChris Wilson * intermediate allocations. 128b94de9bbSChris Wilson */ 129b94de9bbSChris Wilson void *ptr = (void *) __get_free_page(gfp_mask); 130b94de9bbSChris Wilson kmemleak_alloc(ptr, PAGE_SIZE, 1, gfp_mask); 131b94de9bbSChris Wilson return ptr; 132b94de9bbSChris Wilson } else 1330db9299fSJens Axboe return kmalloc(nents * sizeof(struct scatterlist), gfp_mask); 1340db9299fSJens Axboe } 1350db9299fSJens Axboe 1360db9299fSJens Axboe static void sg_kfree(struct scatterlist *sg, unsigned int nents) 1370db9299fSJens Axboe { 138b94de9bbSChris Wilson if (nents == SG_MAX_SINGLE_ALLOC) { 139b94de9bbSChris Wilson kmemleak_free(sg); 1400db9299fSJens Axboe free_page((unsigned long) sg); 141b94de9bbSChris Wilson } else 1420db9299fSJens Axboe kfree(sg); 1430db9299fSJens Axboe } 1440db9299fSJens Axboe 1450db9299fSJens Axboe /** 1460db9299fSJens Axboe * __sg_free_table - Free a previously mapped sg table 1470db9299fSJens Axboe * @table: The sg table header to use 1487cedb1f1SJames Bottomley * @max_ents: The maximum number of entries per single scatterlist 1490db9299fSJens Axboe * @free_fn: Free function 1500db9299fSJens Axboe * 1510db9299fSJens Axboe * Description: 1527cedb1f1SJames Bottomley * Free an sg table previously allocated and setup with 1537cedb1f1SJames Bottomley * __sg_alloc_table(). The @max_ents value must be identical to 1547cedb1f1SJames Bottomley * that previously used with __sg_alloc_table(). 1550db9299fSJens Axboe * 1560db9299fSJens Axboe **/ 1577cedb1f1SJames Bottomley void __sg_free_table(struct sg_table *table, unsigned int max_ents, 1587cedb1f1SJames Bottomley sg_free_fn *free_fn) 1590db9299fSJens Axboe { 1600db9299fSJens Axboe struct scatterlist *sgl, *next; 1610db9299fSJens Axboe 1620db9299fSJens Axboe if (unlikely(!table->sgl)) 1630db9299fSJens Axboe return; 1640db9299fSJens Axboe 1650db9299fSJens Axboe sgl = table->sgl; 1660db9299fSJens Axboe while (table->orig_nents) { 1670db9299fSJens Axboe unsigned int alloc_size = table->orig_nents; 1680db9299fSJens Axboe unsigned int sg_size; 1690db9299fSJens Axboe 1700db9299fSJens Axboe /* 1717cedb1f1SJames Bottomley * If we have more than max_ents segments left, 1720db9299fSJens Axboe * then assign 'next' to the sg table after the current one. 1730db9299fSJens Axboe * sg_size is then one less than alloc size, since the last 1740db9299fSJens Axboe * element is the chain pointer. 1750db9299fSJens Axboe */ 1767cedb1f1SJames Bottomley if (alloc_size > max_ents) { 1777cedb1f1SJames Bottomley next = sg_chain_ptr(&sgl[max_ents - 1]); 1787cedb1f1SJames Bottomley alloc_size = max_ents; 1790db9299fSJens Axboe sg_size = alloc_size - 1; 1800db9299fSJens Axboe } else { 1810db9299fSJens Axboe sg_size = alloc_size; 1820db9299fSJens Axboe next = NULL; 1830db9299fSJens Axboe } 1840db9299fSJens Axboe 1850db9299fSJens Axboe table->orig_nents -= sg_size; 1860db9299fSJens Axboe free_fn(sgl, alloc_size); 1870db9299fSJens Axboe sgl = next; 1880db9299fSJens Axboe } 1890db9299fSJens Axboe 1900db9299fSJens Axboe table->sgl = NULL; 1910db9299fSJens Axboe } 1920db9299fSJens Axboe EXPORT_SYMBOL(__sg_free_table); 1930db9299fSJens Axboe 1940db9299fSJens Axboe /** 1950db9299fSJens Axboe * sg_free_table - Free a previously allocated sg table 1960db9299fSJens Axboe * @table: The mapped sg table header 1970db9299fSJens Axboe * 1980db9299fSJens Axboe **/ 1990db9299fSJens Axboe void sg_free_table(struct sg_table *table) 2000db9299fSJens Axboe { 2017cedb1f1SJames Bottomley __sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree); 2020db9299fSJens Axboe } 2030db9299fSJens Axboe EXPORT_SYMBOL(sg_free_table); 2040db9299fSJens Axboe 2050db9299fSJens Axboe /** 2060db9299fSJens Axboe * __sg_alloc_table - Allocate and initialize an sg table with given allocator 2070db9299fSJens Axboe * @table: The sg table header to use 2080db9299fSJens Axboe * @nents: Number of entries in sg list 2097cedb1f1SJames Bottomley * @max_ents: The maximum number of entries the allocator returns per call 2100db9299fSJens Axboe * @gfp_mask: GFP allocation mask 2110db9299fSJens Axboe * @alloc_fn: Allocator to use 2120db9299fSJens Axboe * 2137cedb1f1SJames Bottomley * Description: 2147cedb1f1SJames Bottomley * This function returns a @table @nents long. The allocator is 2157cedb1f1SJames Bottomley * defined to return scatterlist chunks of maximum size @max_ents. 2167cedb1f1SJames Bottomley * Thus if @nents is bigger than @max_ents, the scatterlists will be 2177cedb1f1SJames Bottomley * chained in units of @max_ents. 2187cedb1f1SJames Bottomley * 2190db9299fSJens Axboe * Notes: 2200db9299fSJens Axboe * If this function returns non-0 (eg failure), the caller must call 2210db9299fSJens Axboe * __sg_free_table() to cleanup any leftover allocations. 2220db9299fSJens Axboe * 2230db9299fSJens Axboe **/ 2247cedb1f1SJames Bottomley int __sg_alloc_table(struct sg_table *table, unsigned int nents, 2257cedb1f1SJames Bottomley unsigned int max_ents, gfp_t gfp_mask, 2260db9299fSJens Axboe sg_alloc_fn *alloc_fn) 2270db9299fSJens Axboe { 2280db9299fSJens Axboe struct scatterlist *sg, *prv; 2290db9299fSJens Axboe unsigned int left; 2300db9299fSJens Axboe 2310db9299fSJens Axboe #ifndef ARCH_HAS_SG_CHAIN 2327cedb1f1SJames Bottomley BUG_ON(nents > max_ents); 2330db9299fSJens Axboe #endif 2340db9299fSJens Axboe 2350db9299fSJens Axboe memset(table, 0, sizeof(*table)); 2360db9299fSJens Axboe 2370db9299fSJens Axboe left = nents; 2380db9299fSJens Axboe prv = NULL; 2390db9299fSJens Axboe do { 2400db9299fSJens Axboe unsigned int sg_size, alloc_size = left; 2410db9299fSJens Axboe 2427cedb1f1SJames Bottomley if (alloc_size > max_ents) { 2437cedb1f1SJames Bottomley alloc_size = max_ents; 2440db9299fSJens Axboe sg_size = alloc_size - 1; 2450db9299fSJens Axboe } else 2460db9299fSJens Axboe sg_size = alloc_size; 2470db9299fSJens Axboe 2480db9299fSJens Axboe left -= sg_size; 2490db9299fSJens Axboe 2500db9299fSJens Axboe sg = alloc_fn(alloc_size, gfp_mask); 251edce6820SJeffrey Carlyle if (unlikely(!sg)) { 252edce6820SJeffrey Carlyle /* 253edce6820SJeffrey Carlyle * Adjust entry count to reflect that the last 254edce6820SJeffrey Carlyle * entry of the previous table won't be used for 255edce6820SJeffrey Carlyle * linkage. Without this, sg_kfree() may get 256edce6820SJeffrey Carlyle * confused. 257edce6820SJeffrey Carlyle */ 258edce6820SJeffrey Carlyle if (prv) 259edce6820SJeffrey Carlyle table->nents = ++table->orig_nents; 260edce6820SJeffrey Carlyle 2610db9299fSJens Axboe return -ENOMEM; 262edce6820SJeffrey Carlyle } 2630db9299fSJens Axboe 2640db9299fSJens Axboe sg_init_table(sg, alloc_size); 2650db9299fSJens Axboe table->nents = table->orig_nents += sg_size; 2660db9299fSJens Axboe 2670db9299fSJens Axboe /* 2680db9299fSJens Axboe * If this is the first mapping, assign the sg table header. 2690db9299fSJens Axboe * If this is not the first mapping, chain previous part. 2700db9299fSJens Axboe */ 2710db9299fSJens Axboe if (prv) 2727cedb1f1SJames Bottomley sg_chain(prv, max_ents, sg); 2730db9299fSJens Axboe else 2740db9299fSJens Axboe table->sgl = sg; 2750db9299fSJens Axboe 2760db9299fSJens Axboe /* 2770db9299fSJens Axboe * If no more entries after this one, mark the end 2780db9299fSJens Axboe */ 2790db9299fSJens Axboe if (!left) 2800db9299fSJens Axboe sg_mark_end(&sg[sg_size - 1]); 2810db9299fSJens Axboe 2820db9299fSJens Axboe /* 2830db9299fSJens Axboe * only really needed for mempool backed sg allocations (like 2840db9299fSJens Axboe * SCSI), a possible improvement here would be to pass the 2850db9299fSJens Axboe * table pointer into the allocator and let that clear these 2860db9299fSJens Axboe * flags 2870db9299fSJens Axboe */ 2880db9299fSJens Axboe gfp_mask &= ~__GFP_WAIT; 2890db9299fSJens Axboe gfp_mask |= __GFP_HIGH; 2900db9299fSJens Axboe prv = sg; 2910db9299fSJens Axboe } while (left); 2920db9299fSJens Axboe 2930db9299fSJens Axboe return 0; 2940db9299fSJens Axboe } 2950db9299fSJens Axboe EXPORT_SYMBOL(__sg_alloc_table); 2960db9299fSJens Axboe 2970db9299fSJens Axboe /** 2980db9299fSJens Axboe * sg_alloc_table - Allocate and initialize an sg table 2990db9299fSJens Axboe * @table: The sg table header to use 3000db9299fSJens Axboe * @nents: Number of entries in sg list 3010db9299fSJens Axboe * @gfp_mask: GFP allocation mask 3020db9299fSJens Axboe * 3030db9299fSJens Axboe * Description: 3040db9299fSJens Axboe * Allocate and initialize an sg table. If @nents@ is larger than 3050db9299fSJens Axboe * SG_MAX_SINGLE_ALLOC a chained sg table will be setup. 3060db9299fSJens Axboe * 3070db9299fSJens Axboe **/ 3080db9299fSJens Axboe int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask) 3090db9299fSJens Axboe { 3100db9299fSJens Axboe int ret; 3110db9299fSJens Axboe 3127cedb1f1SJames Bottomley ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC, 3137cedb1f1SJames Bottomley gfp_mask, sg_kmalloc); 3140db9299fSJens Axboe if (unlikely(ret)) 3157cedb1f1SJames Bottomley __sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree); 3160db9299fSJens Axboe 3170db9299fSJens Axboe return ret; 3180db9299fSJens Axboe } 3190db9299fSJens Axboe EXPORT_SYMBOL(sg_alloc_table); 320b1adaf65SFUJITA Tomonori 321b1adaf65SFUJITA Tomonori /** 322137d3edbSTejun Heo * sg_miter_start - start mapping iteration over a sg list 323137d3edbSTejun Heo * @miter: sg mapping iter to be started 324137d3edbSTejun Heo * @sgl: sg list to iterate over 325137d3edbSTejun Heo * @nents: number of sg entries 326137d3edbSTejun Heo * 327137d3edbSTejun Heo * Description: 328137d3edbSTejun Heo * Starts mapping iterator @miter. 329137d3edbSTejun Heo * 330137d3edbSTejun Heo * Context: 331137d3edbSTejun Heo * Don't care. 332137d3edbSTejun Heo */ 333137d3edbSTejun Heo void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl, 334137d3edbSTejun Heo unsigned int nents, unsigned int flags) 335137d3edbSTejun Heo { 336137d3edbSTejun Heo memset(miter, 0, sizeof(struct sg_mapping_iter)); 337137d3edbSTejun Heo 338137d3edbSTejun Heo miter->__sg = sgl; 339137d3edbSTejun Heo miter->__nents = nents; 340137d3edbSTejun Heo miter->__offset = 0; 3416de7e356SSebastian Andrzej Siewior WARN_ON(!(flags & (SG_MITER_TO_SG | SG_MITER_FROM_SG))); 342137d3edbSTejun Heo miter->__flags = flags; 343137d3edbSTejun Heo } 344137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_start); 345137d3edbSTejun Heo 346137d3edbSTejun Heo /** 347137d3edbSTejun Heo * sg_miter_next - proceed mapping iterator to the next mapping 348137d3edbSTejun Heo * @miter: sg mapping iter to proceed 349137d3edbSTejun Heo * 350137d3edbSTejun Heo * Description: 351137d3edbSTejun Heo * Proceeds @miter@ to the next mapping. @miter@ should have been 352137d3edbSTejun Heo * started using sg_miter_start(). On successful return, 353137d3edbSTejun Heo * @miter@->page, @miter@->addr and @miter@->length point to the 354137d3edbSTejun Heo * current mapping. 355137d3edbSTejun Heo * 356137d3edbSTejun Heo * Context: 357137d3edbSTejun Heo * IRQ disabled if SG_MITER_ATOMIC. IRQ must stay disabled till 358137d3edbSTejun Heo * @miter@ is stopped. May sleep if !SG_MITER_ATOMIC. 359137d3edbSTejun Heo * 360137d3edbSTejun Heo * Returns: 361137d3edbSTejun Heo * true if @miter contains the next mapping. false if end of sg 362137d3edbSTejun Heo * list is reached. 363137d3edbSTejun Heo */ 364137d3edbSTejun Heo bool sg_miter_next(struct sg_mapping_iter *miter) 365137d3edbSTejun Heo { 366137d3edbSTejun Heo unsigned int off, len; 367137d3edbSTejun Heo 368137d3edbSTejun Heo /* check for end and drop resources from the last iteration */ 369137d3edbSTejun Heo if (!miter->__nents) 370137d3edbSTejun Heo return false; 371137d3edbSTejun Heo 372137d3edbSTejun Heo sg_miter_stop(miter); 373137d3edbSTejun Heo 374137d3edbSTejun Heo /* get to the next sg if necessary. __offset is adjusted by stop */ 37523c560a9STejun Heo while (miter->__offset == miter->__sg->length) { 37623c560a9STejun Heo if (--miter->__nents) { 377137d3edbSTejun Heo miter->__sg = sg_next(miter->__sg); 378137d3edbSTejun Heo miter->__offset = 0; 37923c560a9STejun Heo } else 38023c560a9STejun Heo return false; 381137d3edbSTejun Heo } 382137d3edbSTejun Heo 383137d3edbSTejun Heo /* map the next page */ 384137d3edbSTejun Heo off = miter->__sg->offset + miter->__offset; 385137d3edbSTejun Heo len = miter->__sg->length - miter->__offset; 386137d3edbSTejun Heo 387137d3edbSTejun Heo miter->page = nth_page(sg_page(miter->__sg), off >> PAGE_SHIFT); 388137d3edbSTejun Heo off &= ~PAGE_MASK; 389137d3edbSTejun Heo miter->length = min_t(unsigned int, len, PAGE_SIZE - off); 390137d3edbSTejun Heo miter->consumed = miter->length; 391137d3edbSTejun Heo 392137d3edbSTejun Heo if (miter->__flags & SG_MITER_ATOMIC) 393137d3edbSTejun Heo miter->addr = kmap_atomic(miter->page, KM_BIO_SRC_IRQ) + off; 394137d3edbSTejun Heo else 395137d3edbSTejun Heo miter->addr = kmap(miter->page) + off; 396137d3edbSTejun Heo 397137d3edbSTejun Heo return true; 398137d3edbSTejun Heo } 399137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_next); 400137d3edbSTejun Heo 401137d3edbSTejun Heo /** 402137d3edbSTejun Heo * sg_miter_stop - stop mapping iteration 403137d3edbSTejun Heo * @miter: sg mapping iter to be stopped 404137d3edbSTejun Heo * 405137d3edbSTejun Heo * Description: 406137d3edbSTejun Heo * Stops mapping iterator @miter. @miter should have been started 407137d3edbSTejun Heo * started using sg_miter_start(). A stopped iteration can be 408137d3edbSTejun Heo * resumed by calling sg_miter_next() on it. This is useful when 409137d3edbSTejun Heo * resources (kmap) need to be released during iteration. 410137d3edbSTejun Heo * 411137d3edbSTejun Heo * Context: 412137d3edbSTejun Heo * IRQ disabled if the SG_MITER_ATOMIC is set. Don't care otherwise. 413137d3edbSTejun Heo */ 414137d3edbSTejun Heo void sg_miter_stop(struct sg_mapping_iter *miter) 415137d3edbSTejun Heo { 416137d3edbSTejun Heo WARN_ON(miter->consumed > miter->length); 417137d3edbSTejun Heo 418137d3edbSTejun Heo /* drop resources from the last iteration */ 419137d3edbSTejun Heo if (miter->addr) { 420137d3edbSTejun Heo miter->__offset += miter->consumed; 421137d3edbSTejun Heo 4226de7e356SSebastian Andrzej Siewior if (miter->__flags & SG_MITER_TO_SG) 4236de7e356SSebastian Andrzej Siewior flush_kernel_dcache_page(miter->page); 4246de7e356SSebastian Andrzej Siewior 425137d3edbSTejun Heo if (miter->__flags & SG_MITER_ATOMIC) { 426137d3edbSTejun Heo WARN_ON(!irqs_disabled()); 427137d3edbSTejun Heo kunmap_atomic(miter->addr, KM_BIO_SRC_IRQ); 428137d3edbSTejun Heo } else 429f652c521SArjan van de Ven kunmap(miter->page); 430137d3edbSTejun Heo 431137d3edbSTejun Heo miter->page = NULL; 432137d3edbSTejun Heo miter->addr = NULL; 433137d3edbSTejun Heo miter->length = 0; 434137d3edbSTejun Heo miter->consumed = 0; 435137d3edbSTejun Heo } 436137d3edbSTejun Heo } 437137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_stop); 438137d3edbSTejun Heo 439137d3edbSTejun Heo /** 440b1adaf65SFUJITA Tomonori * sg_copy_buffer - Copy data between a linear buffer and an SG list 441b1adaf65SFUJITA Tomonori * @sgl: The SG list 442b1adaf65SFUJITA Tomonori * @nents: Number of SG entries 443b1adaf65SFUJITA Tomonori * @buf: Where to copy from 444b1adaf65SFUJITA Tomonori * @buflen: The number of bytes to copy 445b1adaf65SFUJITA Tomonori * @to_buffer: transfer direction (non zero == from an sg list to a 446b1adaf65SFUJITA Tomonori * buffer, 0 == from a buffer to an sg list 447b1adaf65SFUJITA Tomonori * 448b1adaf65SFUJITA Tomonori * Returns the number of copied bytes. 449b1adaf65SFUJITA Tomonori * 450b1adaf65SFUJITA Tomonori **/ 451b1adaf65SFUJITA Tomonori static size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, 452b1adaf65SFUJITA Tomonori void *buf, size_t buflen, int to_buffer) 453b1adaf65SFUJITA Tomonori { 454137d3edbSTejun Heo unsigned int offset = 0; 455137d3edbSTejun Heo struct sg_mapping_iter miter; 45650bed2e2SFUJITA Tomonori unsigned long flags; 4576de7e356SSebastian Andrzej Siewior unsigned int sg_flags = SG_MITER_ATOMIC; 458b1adaf65SFUJITA Tomonori 4596de7e356SSebastian Andrzej Siewior if (to_buffer) 4606de7e356SSebastian Andrzej Siewior sg_flags |= SG_MITER_FROM_SG; 4616de7e356SSebastian Andrzej Siewior else 4626de7e356SSebastian Andrzej Siewior sg_flags |= SG_MITER_TO_SG; 4636de7e356SSebastian Andrzej Siewior 4646de7e356SSebastian Andrzej Siewior sg_miter_start(&miter, sgl, nents, sg_flags); 465b1adaf65SFUJITA Tomonori 46650bed2e2SFUJITA Tomonori local_irq_save(flags); 46750bed2e2SFUJITA Tomonori 468137d3edbSTejun Heo while (sg_miter_next(&miter) && offset < buflen) { 469137d3edbSTejun Heo unsigned int len; 470b1adaf65SFUJITA Tomonori 471137d3edbSTejun Heo len = min(miter.length, buflen - offset); 472b1adaf65SFUJITA Tomonori 473b1adaf65SFUJITA Tomonori if (to_buffer) 474137d3edbSTejun Heo memcpy(buf + offset, miter.addr, len); 4756de7e356SSebastian Andrzej Siewior else 476137d3edbSTejun Heo memcpy(miter.addr, buf + offset, len); 477b1adaf65SFUJITA Tomonori 478137d3edbSTejun Heo offset += len; 479b1adaf65SFUJITA Tomonori } 480b1adaf65SFUJITA Tomonori 481137d3edbSTejun Heo sg_miter_stop(&miter); 482b1adaf65SFUJITA Tomonori 48350bed2e2SFUJITA Tomonori local_irq_restore(flags); 484137d3edbSTejun Heo return offset; 485b1adaf65SFUJITA Tomonori } 486b1adaf65SFUJITA Tomonori 487b1adaf65SFUJITA Tomonori /** 488b1adaf65SFUJITA Tomonori * sg_copy_from_buffer - Copy from a linear buffer to an SG list 489b1adaf65SFUJITA Tomonori * @sgl: The SG list 490b1adaf65SFUJITA Tomonori * @nents: Number of SG entries 491b1adaf65SFUJITA Tomonori * @buf: Where to copy from 492b1adaf65SFUJITA Tomonori * @buflen: The number of bytes to copy 493b1adaf65SFUJITA Tomonori * 494b1adaf65SFUJITA Tomonori * Returns the number of copied bytes. 495b1adaf65SFUJITA Tomonori * 496b1adaf65SFUJITA Tomonori **/ 497b1adaf65SFUJITA Tomonori size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents, 498b1adaf65SFUJITA Tomonori void *buf, size_t buflen) 499b1adaf65SFUJITA Tomonori { 500b1adaf65SFUJITA Tomonori return sg_copy_buffer(sgl, nents, buf, buflen, 0); 501b1adaf65SFUJITA Tomonori } 502b1adaf65SFUJITA Tomonori EXPORT_SYMBOL(sg_copy_from_buffer); 503b1adaf65SFUJITA Tomonori 504b1adaf65SFUJITA Tomonori /** 505b1adaf65SFUJITA Tomonori * sg_copy_to_buffer - Copy from an SG list to a linear buffer 506b1adaf65SFUJITA Tomonori * @sgl: The SG list 507b1adaf65SFUJITA Tomonori * @nents: Number of SG entries 508b1adaf65SFUJITA Tomonori * @buf: Where to copy to 509b1adaf65SFUJITA Tomonori * @buflen: The number of bytes to copy 510b1adaf65SFUJITA Tomonori * 511b1adaf65SFUJITA Tomonori * Returns the number of copied bytes. 512b1adaf65SFUJITA Tomonori * 513b1adaf65SFUJITA Tomonori **/ 514b1adaf65SFUJITA Tomonori size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents, 515b1adaf65SFUJITA Tomonori void *buf, size_t buflen) 516b1adaf65SFUJITA Tomonori { 517b1adaf65SFUJITA Tomonori return sg_copy_buffer(sgl, nents, buf, buflen, 1); 518b1adaf65SFUJITA Tomonori } 519b1adaf65SFUJITA Tomonori EXPORT_SYMBOL(sg_copy_to_buffer); 520