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); 2510db9299fSJens Axboe if (unlikely(!sg)) 2520db9299fSJens Axboe return -ENOMEM; 2530db9299fSJens Axboe 2540db9299fSJens Axboe sg_init_table(sg, alloc_size); 2550db9299fSJens Axboe table->nents = table->orig_nents += sg_size; 2560db9299fSJens Axboe 2570db9299fSJens Axboe /* 2580db9299fSJens Axboe * If this is the first mapping, assign the sg table header. 2590db9299fSJens Axboe * If this is not the first mapping, chain previous part. 2600db9299fSJens Axboe */ 2610db9299fSJens Axboe if (prv) 2627cedb1f1SJames Bottomley sg_chain(prv, max_ents, sg); 2630db9299fSJens Axboe else 2640db9299fSJens Axboe table->sgl = sg; 2650db9299fSJens Axboe 2660db9299fSJens Axboe /* 2670db9299fSJens Axboe * If no more entries after this one, mark the end 2680db9299fSJens Axboe */ 2690db9299fSJens Axboe if (!left) 2700db9299fSJens Axboe sg_mark_end(&sg[sg_size - 1]); 2710db9299fSJens Axboe 2720db9299fSJens Axboe /* 2730db9299fSJens Axboe * only really needed for mempool backed sg allocations (like 2740db9299fSJens Axboe * SCSI), a possible improvement here would be to pass the 2750db9299fSJens Axboe * table pointer into the allocator and let that clear these 2760db9299fSJens Axboe * flags 2770db9299fSJens Axboe */ 2780db9299fSJens Axboe gfp_mask &= ~__GFP_WAIT; 2790db9299fSJens Axboe gfp_mask |= __GFP_HIGH; 2800db9299fSJens Axboe prv = sg; 2810db9299fSJens Axboe } while (left); 2820db9299fSJens Axboe 2830db9299fSJens Axboe return 0; 2840db9299fSJens Axboe } 2850db9299fSJens Axboe EXPORT_SYMBOL(__sg_alloc_table); 2860db9299fSJens Axboe 2870db9299fSJens Axboe /** 2880db9299fSJens Axboe * sg_alloc_table - Allocate and initialize an sg table 2890db9299fSJens Axboe * @table: The sg table header to use 2900db9299fSJens Axboe * @nents: Number of entries in sg list 2910db9299fSJens Axboe * @gfp_mask: GFP allocation mask 2920db9299fSJens Axboe * 2930db9299fSJens Axboe * Description: 2940db9299fSJens Axboe * Allocate and initialize an sg table. If @nents@ is larger than 2950db9299fSJens Axboe * SG_MAX_SINGLE_ALLOC a chained sg table will be setup. 2960db9299fSJens Axboe * 2970db9299fSJens Axboe **/ 2980db9299fSJens Axboe int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask) 2990db9299fSJens Axboe { 3000db9299fSJens Axboe int ret; 3010db9299fSJens Axboe 3027cedb1f1SJames Bottomley ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC, 3037cedb1f1SJames Bottomley gfp_mask, sg_kmalloc); 3040db9299fSJens Axboe if (unlikely(ret)) 3057cedb1f1SJames Bottomley __sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree); 3060db9299fSJens Axboe 3070db9299fSJens Axboe return ret; 3080db9299fSJens Axboe } 3090db9299fSJens Axboe EXPORT_SYMBOL(sg_alloc_table); 310b1adaf65SFUJITA Tomonori 311b1adaf65SFUJITA Tomonori /** 312137d3edbSTejun Heo * sg_miter_start - start mapping iteration over a sg list 313137d3edbSTejun Heo * @miter: sg mapping iter to be started 314137d3edbSTejun Heo * @sgl: sg list to iterate over 315137d3edbSTejun Heo * @nents: number of sg entries 316137d3edbSTejun Heo * 317137d3edbSTejun Heo * Description: 318137d3edbSTejun Heo * Starts mapping iterator @miter. 319137d3edbSTejun Heo * 320137d3edbSTejun Heo * Context: 321137d3edbSTejun Heo * Don't care. 322137d3edbSTejun Heo */ 323137d3edbSTejun Heo void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl, 324137d3edbSTejun Heo unsigned int nents, unsigned int flags) 325137d3edbSTejun Heo { 326137d3edbSTejun Heo memset(miter, 0, sizeof(struct sg_mapping_iter)); 327137d3edbSTejun Heo 328137d3edbSTejun Heo miter->__sg = sgl; 329137d3edbSTejun Heo miter->__nents = nents; 330137d3edbSTejun Heo miter->__offset = 0; 3316de7e356SSebastian Andrzej Siewior WARN_ON(!(flags & (SG_MITER_TO_SG | SG_MITER_FROM_SG))); 332137d3edbSTejun Heo miter->__flags = flags; 333137d3edbSTejun Heo } 334137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_start); 335137d3edbSTejun Heo 336137d3edbSTejun Heo /** 337137d3edbSTejun Heo * sg_miter_next - proceed mapping iterator to the next mapping 338137d3edbSTejun Heo * @miter: sg mapping iter to proceed 339137d3edbSTejun Heo * 340137d3edbSTejun Heo * Description: 341137d3edbSTejun Heo * Proceeds @miter@ to the next mapping. @miter@ should have been 342137d3edbSTejun Heo * started using sg_miter_start(). On successful return, 343137d3edbSTejun Heo * @miter@->page, @miter@->addr and @miter@->length point to the 344137d3edbSTejun Heo * current mapping. 345137d3edbSTejun Heo * 346137d3edbSTejun Heo * Context: 347137d3edbSTejun Heo * IRQ disabled if SG_MITER_ATOMIC. IRQ must stay disabled till 348137d3edbSTejun Heo * @miter@ is stopped. May sleep if !SG_MITER_ATOMIC. 349137d3edbSTejun Heo * 350137d3edbSTejun Heo * Returns: 351137d3edbSTejun Heo * true if @miter contains the next mapping. false if end of sg 352137d3edbSTejun Heo * list is reached. 353137d3edbSTejun Heo */ 354137d3edbSTejun Heo bool sg_miter_next(struct sg_mapping_iter *miter) 355137d3edbSTejun Heo { 356137d3edbSTejun Heo unsigned int off, len; 357137d3edbSTejun Heo 358137d3edbSTejun Heo /* check for end and drop resources from the last iteration */ 359137d3edbSTejun Heo if (!miter->__nents) 360137d3edbSTejun Heo return false; 361137d3edbSTejun Heo 362137d3edbSTejun Heo sg_miter_stop(miter); 363137d3edbSTejun Heo 364137d3edbSTejun Heo /* get to the next sg if necessary. __offset is adjusted by stop */ 36523c560a9STejun Heo while (miter->__offset == miter->__sg->length) { 36623c560a9STejun Heo if (--miter->__nents) { 367137d3edbSTejun Heo miter->__sg = sg_next(miter->__sg); 368137d3edbSTejun Heo miter->__offset = 0; 36923c560a9STejun Heo } else 37023c560a9STejun Heo return false; 371137d3edbSTejun Heo } 372137d3edbSTejun Heo 373137d3edbSTejun Heo /* map the next page */ 374137d3edbSTejun Heo off = miter->__sg->offset + miter->__offset; 375137d3edbSTejun Heo len = miter->__sg->length - miter->__offset; 376137d3edbSTejun Heo 377137d3edbSTejun Heo miter->page = nth_page(sg_page(miter->__sg), off >> PAGE_SHIFT); 378137d3edbSTejun Heo off &= ~PAGE_MASK; 379137d3edbSTejun Heo miter->length = min_t(unsigned int, len, PAGE_SIZE - off); 380137d3edbSTejun Heo miter->consumed = miter->length; 381137d3edbSTejun Heo 382137d3edbSTejun Heo if (miter->__flags & SG_MITER_ATOMIC) 383137d3edbSTejun Heo miter->addr = kmap_atomic(miter->page, KM_BIO_SRC_IRQ) + off; 384137d3edbSTejun Heo else 385137d3edbSTejun Heo miter->addr = kmap(miter->page) + off; 386137d3edbSTejun Heo 387137d3edbSTejun Heo return true; 388137d3edbSTejun Heo } 389137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_next); 390137d3edbSTejun Heo 391137d3edbSTejun Heo /** 392137d3edbSTejun Heo * sg_miter_stop - stop mapping iteration 393137d3edbSTejun Heo * @miter: sg mapping iter to be stopped 394137d3edbSTejun Heo * 395137d3edbSTejun Heo * Description: 396137d3edbSTejun Heo * Stops mapping iterator @miter. @miter should have been started 397137d3edbSTejun Heo * started using sg_miter_start(). A stopped iteration can be 398137d3edbSTejun Heo * resumed by calling sg_miter_next() on it. This is useful when 399137d3edbSTejun Heo * resources (kmap) need to be released during iteration. 400137d3edbSTejun Heo * 401137d3edbSTejun Heo * Context: 402137d3edbSTejun Heo * IRQ disabled if the SG_MITER_ATOMIC is set. Don't care otherwise. 403137d3edbSTejun Heo */ 404137d3edbSTejun Heo void sg_miter_stop(struct sg_mapping_iter *miter) 405137d3edbSTejun Heo { 406137d3edbSTejun Heo WARN_ON(miter->consumed > miter->length); 407137d3edbSTejun Heo 408137d3edbSTejun Heo /* drop resources from the last iteration */ 409137d3edbSTejun Heo if (miter->addr) { 410137d3edbSTejun Heo miter->__offset += miter->consumed; 411137d3edbSTejun Heo 4126de7e356SSebastian Andrzej Siewior if (miter->__flags & SG_MITER_TO_SG) 4136de7e356SSebastian Andrzej Siewior flush_kernel_dcache_page(miter->page); 4146de7e356SSebastian Andrzej Siewior 415137d3edbSTejun Heo if (miter->__flags & SG_MITER_ATOMIC) { 416137d3edbSTejun Heo WARN_ON(!irqs_disabled()); 417137d3edbSTejun Heo kunmap_atomic(miter->addr, KM_BIO_SRC_IRQ); 418137d3edbSTejun Heo } else 419f652c521SArjan van de Ven kunmap(miter->page); 420137d3edbSTejun Heo 421137d3edbSTejun Heo miter->page = NULL; 422137d3edbSTejun Heo miter->addr = NULL; 423137d3edbSTejun Heo miter->length = 0; 424137d3edbSTejun Heo miter->consumed = 0; 425137d3edbSTejun Heo } 426137d3edbSTejun Heo } 427137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_stop); 428137d3edbSTejun Heo 429137d3edbSTejun Heo /** 430b1adaf65SFUJITA Tomonori * sg_copy_buffer - Copy data between a linear buffer and an SG list 431b1adaf65SFUJITA Tomonori * @sgl: The SG list 432b1adaf65SFUJITA Tomonori * @nents: Number of SG entries 433b1adaf65SFUJITA Tomonori * @buf: Where to copy from 434b1adaf65SFUJITA Tomonori * @buflen: The number of bytes to copy 435b1adaf65SFUJITA Tomonori * @to_buffer: transfer direction (non zero == from an sg list to a 436b1adaf65SFUJITA Tomonori * buffer, 0 == from a buffer to an sg list 437b1adaf65SFUJITA Tomonori * 438b1adaf65SFUJITA Tomonori * Returns the number of copied bytes. 439b1adaf65SFUJITA Tomonori * 440b1adaf65SFUJITA Tomonori **/ 441b1adaf65SFUJITA Tomonori static size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, 442b1adaf65SFUJITA Tomonori void *buf, size_t buflen, int to_buffer) 443b1adaf65SFUJITA Tomonori { 444137d3edbSTejun Heo unsigned int offset = 0; 445137d3edbSTejun Heo struct sg_mapping_iter miter; 44650bed2e2SFUJITA Tomonori unsigned long flags; 4476de7e356SSebastian Andrzej Siewior unsigned int sg_flags = SG_MITER_ATOMIC; 448b1adaf65SFUJITA Tomonori 4496de7e356SSebastian Andrzej Siewior if (to_buffer) 4506de7e356SSebastian Andrzej Siewior sg_flags |= SG_MITER_FROM_SG; 4516de7e356SSebastian Andrzej Siewior else 4526de7e356SSebastian Andrzej Siewior sg_flags |= SG_MITER_TO_SG; 4536de7e356SSebastian Andrzej Siewior 4546de7e356SSebastian Andrzej Siewior sg_miter_start(&miter, sgl, nents, sg_flags); 455b1adaf65SFUJITA Tomonori 45650bed2e2SFUJITA Tomonori local_irq_save(flags); 45750bed2e2SFUJITA Tomonori 458137d3edbSTejun Heo while (sg_miter_next(&miter) && offset < buflen) { 459137d3edbSTejun Heo unsigned int len; 460b1adaf65SFUJITA Tomonori 461137d3edbSTejun Heo len = min(miter.length, buflen - offset); 462b1adaf65SFUJITA Tomonori 463b1adaf65SFUJITA Tomonori if (to_buffer) 464137d3edbSTejun Heo memcpy(buf + offset, miter.addr, len); 4656de7e356SSebastian Andrzej Siewior else 466137d3edbSTejun Heo memcpy(miter.addr, buf + offset, len); 467b1adaf65SFUJITA Tomonori 468137d3edbSTejun Heo offset += len; 469b1adaf65SFUJITA Tomonori } 470b1adaf65SFUJITA Tomonori 471137d3edbSTejun Heo sg_miter_stop(&miter); 472b1adaf65SFUJITA Tomonori 47350bed2e2SFUJITA Tomonori local_irq_restore(flags); 474137d3edbSTejun Heo return offset; 475b1adaf65SFUJITA Tomonori } 476b1adaf65SFUJITA Tomonori 477b1adaf65SFUJITA Tomonori /** 478b1adaf65SFUJITA Tomonori * sg_copy_from_buffer - Copy from a linear buffer to an SG list 479b1adaf65SFUJITA Tomonori * @sgl: The SG list 480b1adaf65SFUJITA Tomonori * @nents: Number of SG entries 481b1adaf65SFUJITA Tomonori * @buf: Where to copy from 482b1adaf65SFUJITA Tomonori * @buflen: The number of bytes to copy 483b1adaf65SFUJITA Tomonori * 484b1adaf65SFUJITA Tomonori * Returns the number of copied bytes. 485b1adaf65SFUJITA Tomonori * 486b1adaf65SFUJITA Tomonori **/ 487b1adaf65SFUJITA Tomonori size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents, 488b1adaf65SFUJITA Tomonori void *buf, size_t buflen) 489b1adaf65SFUJITA Tomonori { 490b1adaf65SFUJITA Tomonori return sg_copy_buffer(sgl, nents, buf, buflen, 0); 491b1adaf65SFUJITA Tomonori } 492b1adaf65SFUJITA Tomonori EXPORT_SYMBOL(sg_copy_from_buffer); 493b1adaf65SFUJITA Tomonori 494b1adaf65SFUJITA Tomonori /** 495b1adaf65SFUJITA Tomonori * sg_copy_to_buffer - Copy from an SG list to a linear buffer 496b1adaf65SFUJITA Tomonori * @sgl: The SG list 497b1adaf65SFUJITA Tomonori * @nents: Number of SG entries 498b1adaf65SFUJITA Tomonori * @buf: Where to copy to 499b1adaf65SFUJITA Tomonori * @buflen: The number of bytes to copy 500b1adaf65SFUJITA Tomonori * 501b1adaf65SFUJITA Tomonori * Returns the number of copied bytes. 502b1adaf65SFUJITA Tomonori * 503b1adaf65SFUJITA Tomonori **/ 504b1adaf65SFUJITA Tomonori size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents, 505b1adaf65SFUJITA Tomonori void *buf, size_t buflen) 506b1adaf65SFUJITA Tomonori { 507b1adaf65SFUJITA Tomonori return sg_copy_buffer(sgl, nents, buf, buflen, 1); 508b1adaf65SFUJITA Tomonori } 509b1adaf65SFUJITA Tomonori EXPORT_SYMBOL(sg_copy_to_buffer); 510