xref: /openbmc/linux/lib/scatterlist.c (revision e80a0af4)
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  */
98bc3bcc9SPaul Gortmaker #include <linux/export.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 /**
422e484610SMaxim Levitsky  * sg_nents - return total count of entries in scatterlist
432e484610SMaxim Levitsky  * @sg:		The scatterlist
442e484610SMaxim Levitsky  *
452e484610SMaxim Levitsky  * Description:
462e484610SMaxim Levitsky  * Allows to know how many entries are in sg, taking into acount
472e484610SMaxim Levitsky  * chaining as well
482e484610SMaxim Levitsky  *
492e484610SMaxim Levitsky  **/
502e484610SMaxim Levitsky int sg_nents(struct scatterlist *sg)
512e484610SMaxim Levitsky {
52232f1b51SMaxim Levitsky 	int nents;
53232f1b51SMaxim Levitsky 	for (nents = 0; sg; sg = sg_next(sg))
542e484610SMaxim Levitsky 		nents++;
552e484610SMaxim Levitsky 	return nents;
562e484610SMaxim Levitsky }
572e484610SMaxim Levitsky EXPORT_SYMBOL(sg_nents);
582e484610SMaxim Levitsky 
59cfaed10dSTom Lendacky /**
60cfaed10dSTom Lendacky  * sg_nents_for_len - return total count of entries in scatterlist
61cfaed10dSTom Lendacky  *                    needed to satisfy the supplied length
62cfaed10dSTom Lendacky  * @sg:		The scatterlist
63cfaed10dSTom Lendacky  * @len:	The total required length
64cfaed10dSTom Lendacky  *
65cfaed10dSTom Lendacky  * Description:
66cfaed10dSTom Lendacky  * Determines the number of entries in sg that are required to meet
67cfaed10dSTom Lendacky  * the supplied length, taking into acount chaining as well
68cfaed10dSTom Lendacky  *
69cfaed10dSTom Lendacky  * Returns:
70cfaed10dSTom Lendacky  *   the number of sg entries needed, negative error on failure
71cfaed10dSTom Lendacky  *
72cfaed10dSTom Lendacky  **/
73cfaed10dSTom Lendacky int sg_nents_for_len(struct scatterlist *sg, u64 len)
74cfaed10dSTom Lendacky {
75cfaed10dSTom Lendacky 	int nents;
76cfaed10dSTom Lendacky 	u64 total;
77cfaed10dSTom Lendacky 
78cfaed10dSTom Lendacky 	if (!len)
79cfaed10dSTom Lendacky 		return 0;
80cfaed10dSTom Lendacky 
81cfaed10dSTom Lendacky 	for (nents = 0, total = 0; sg; sg = sg_next(sg)) {
82cfaed10dSTom Lendacky 		nents++;
83cfaed10dSTom Lendacky 		total += sg->length;
84cfaed10dSTom Lendacky 		if (total >= len)
85cfaed10dSTom Lendacky 			return nents;
86cfaed10dSTom Lendacky 	}
87cfaed10dSTom Lendacky 
88cfaed10dSTom Lendacky 	return -EINVAL;
89cfaed10dSTom Lendacky }
90cfaed10dSTom Lendacky EXPORT_SYMBOL(sg_nents_for_len);
912e484610SMaxim Levitsky 
922e484610SMaxim Levitsky /**
930db9299fSJens Axboe  * sg_last - return the last scatterlist entry in a list
940db9299fSJens Axboe  * @sgl:	First entry in the scatterlist
950db9299fSJens Axboe  * @nents:	Number of entries in the scatterlist
960db9299fSJens Axboe  *
970db9299fSJens Axboe  * Description:
980db9299fSJens Axboe  *   Should only be used casually, it (currently) scans the entire list
990db9299fSJens Axboe  *   to get the last entry.
1000db9299fSJens Axboe  *
1010db9299fSJens Axboe  *   Note that the @sgl@ pointer passed in need not be the first one,
1020db9299fSJens Axboe  *   the important bit is that @nents@ denotes the number of entries that
1030db9299fSJens Axboe  *   exist from @sgl@.
1040db9299fSJens Axboe  *
1050db9299fSJens Axboe  **/
1060db9299fSJens Axboe struct scatterlist *sg_last(struct scatterlist *sgl, unsigned int nents)
1070db9299fSJens Axboe {
1080db9299fSJens Axboe 	struct scatterlist *sg, *ret = NULL;
1090db9299fSJens Axboe 	unsigned int i;
1100db9299fSJens Axboe 
1110db9299fSJens Axboe 	for_each_sg(sgl, sg, nents, i)
1120db9299fSJens Axboe 		ret = sg;
1130db9299fSJens Axboe 
1140db9299fSJens Axboe #ifdef CONFIG_DEBUG_SG
1150db9299fSJens Axboe 	BUG_ON(sgl[0].sg_magic != SG_MAGIC);
1160db9299fSJens Axboe 	BUG_ON(!sg_is_last(ret));
1170db9299fSJens Axboe #endif
1180db9299fSJens Axboe 	return ret;
1190db9299fSJens Axboe }
1200db9299fSJens Axboe EXPORT_SYMBOL(sg_last);
1210db9299fSJens Axboe 
1220db9299fSJens Axboe /**
1230db9299fSJens Axboe  * sg_init_table - Initialize SG table
1240db9299fSJens Axboe  * @sgl:	   The SG table
1250db9299fSJens Axboe  * @nents:	   Number of entries in table
1260db9299fSJens Axboe  *
1270db9299fSJens Axboe  * Notes:
1280db9299fSJens Axboe  *   If this is part of a chained sg table, sg_mark_end() should be
1290db9299fSJens Axboe  *   used only on the last table part.
1300db9299fSJens Axboe  *
1310db9299fSJens Axboe  **/
1320db9299fSJens Axboe void sg_init_table(struct scatterlist *sgl, unsigned int nents)
1330db9299fSJens Axboe {
1340db9299fSJens Axboe 	memset(sgl, 0, sizeof(*sgl) * nents);
1350db9299fSJens Axboe #ifdef CONFIG_DEBUG_SG
1360db9299fSJens Axboe 	{
1370db9299fSJens Axboe 		unsigned int i;
1380db9299fSJens Axboe 		for (i = 0; i < nents; i++)
1390db9299fSJens Axboe 			sgl[i].sg_magic = SG_MAGIC;
1400db9299fSJens Axboe 	}
1410db9299fSJens Axboe #endif
1420db9299fSJens Axboe 	sg_mark_end(&sgl[nents - 1]);
1430db9299fSJens Axboe }
1440db9299fSJens Axboe EXPORT_SYMBOL(sg_init_table);
1450db9299fSJens Axboe 
1460db9299fSJens Axboe /**
1470db9299fSJens Axboe  * sg_init_one - Initialize a single entry sg list
1480db9299fSJens Axboe  * @sg:		 SG entry
1490db9299fSJens Axboe  * @buf:	 Virtual address for IO
1500db9299fSJens Axboe  * @buflen:	 IO length
1510db9299fSJens Axboe  *
1520db9299fSJens Axboe  **/
1530db9299fSJens Axboe void sg_init_one(struct scatterlist *sg, const void *buf, unsigned int buflen)
1540db9299fSJens Axboe {
1550db9299fSJens Axboe 	sg_init_table(sg, 1);
1560db9299fSJens Axboe 	sg_set_buf(sg, buf, buflen);
1570db9299fSJens Axboe }
1580db9299fSJens Axboe EXPORT_SYMBOL(sg_init_one);
1590db9299fSJens Axboe 
1600db9299fSJens Axboe /*
1610db9299fSJens Axboe  * The default behaviour of sg_alloc_table() is to use these kmalloc/kfree
1620db9299fSJens Axboe  * helpers.
1630db9299fSJens Axboe  */
1640db9299fSJens Axboe static struct scatterlist *sg_kmalloc(unsigned int nents, gfp_t gfp_mask)
1650db9299fSJens Axboe {
166b94de9bbSChris Wilson 	if (nents == SG_MAX_SINGLE_ALLOC) {
167b94de9bbSChris Wilson 		/*
168b94de9bbSChris Wilson 		 * Kmemleak doesn't track page allocations as they are not
169b94de9bbSChris Wilson 		 * commonly used (in a raw form) for kernel data structures.
170b94de9bbSChris Wilson 		 * As we chain together a list of pages and then a normal
171b94de9bbSChris Wilson 		 * kmalloc (tracked by kmemleak), in order to for that last
172b94de9bbSChris Wilson 		 * allocation not to become decoupled (and thus a
173b94de9bbSChris Wilson 		 * false-positive) we need to inform kmemleak of all the
174b94de9bbSChris Wilson 		 * intermediate allocations.
175b94de9bbSChris Wilson 		 */
176b94de9bbSChris Wilson 		void *ptr = (void *) __get_free_page(gfp_mask);
177b94de9bbSChris Wilson 		kmemleak_alloc(ptr, PAGE_SIZE, 1, gfp_mask);
178b94de9bbSChris Wilson 		return ptr;
179b94de9bbSChris Wilson 	} else
1800db9299fSJens Axboe 		return kmalloc(nents * sizeof(struct scatterlist), gfp_mask);
1810db9299fSJens Axboe }
1820db9299fSJens Axboe 
1830db9299fSJens Axboe static void sg_kfree(struct scatterlist *sg, unsigned int nents)
1840db9299fSJens Axboe {
185b94de9bbSChris Wilson 	if (nents == SG_MAX_SINGLE_ALLOC) {
186b94de9bbSChris Wilson 		kmemleak_free(sg);
1870db9299fSJens Axboe 		free_page((unsigned long) sg);
188b94de9bbSChris Wilson 	} else
1890db9299fSJens Axboe 		kfree(sg);
1900db9299fSJens Axboe }
1910db9299fSJens Axboe 
1920db9299fSJens Axboe /**
1930db9299fSJens Axboe  * __sg_free_table - Free a previously mapped sg table
1940db9299fSJens Axboe  * @table:	The sg table header to use
1957cedb1f1SJames Bottomley  * @max_ents:	The maximum number of entries per single scatterlist
196c53c6d6aSChristoph Hellwig  * @skip_first_chunk: don't free the (preallocated) first scatterlist chunk
1970db9299fSJens Axboe  * @free_fn:	Free function
1980db9299fSJens Axboe  *
1990db9299fSJens Axboe  *  Description:
2007cedb1f1SJames Bottomley  *    Free an sg table previously allocated and setup with
2017cedb1f1SJames Bottomley  *    __sg_alloc_table().  The @max_ents value must be identical to
2027cedb1f1SJames Bottomley  *    that previously used with __sg_alloc_table().
2030db9299fSJens Axboe  *
2040db9299fSJens Axboe  **/
2057cedb1f1SJames Bottomley void __sg_free_table(struct sg_table *table, unsigned int max_ents,
206c53c6d6aSChristoph Hellwig 		     bool skip_first_chunk, sg_free_fn *free_fn)
2070db9299fSJens Axboe {
2080db9299fSJens Axboe 	struct scatterlist *sgl, *next;
2090db9299fSJens Axboe 
2100db9299fSJens Axboe 	if (unlikely(!table->sgl))
2110db9299fSJens Axboe 		return;
2120db9299fSJens Axboe 
2130db9299fSJens Axboe 	sgl = table->sgl;
2140db9299fSJens Axboe 	while (table->orig_nents) {
2150db9299fSJens Axboe 		unsigned int alloc_size = table->orig_nents;
2160db9299fSJens Axboe 		unsigned int sg_size;
2170db9299fSJens Axboe 
2180db9299fSJens Axboe 		/*
2197cedb1f1SJames Bottomley 		 * If we have more than max_ents segments left,
2200db9299fSJens Axboe 		 * then assign 'next' to the sg table after the current one.
2210db9299fSJens Axboe 		 * sg_size is then one less than alloc size, since the last
2220db9299fSJens Axboe 		 * element is the chain pointer.
2230db9299fSJens Axboe 		 */
2247cedb1f1SJames Bottomley 		if (alloc_size > max_ents) {
2257cedb1f1SJames Bottomley 			next = sg_chain_ptr(&sgl[max_ents - 1]);
2267cedb1f1SJames Bottomley 			alloc_size = max_ents;
2270db9299fSJens Axboe 			sg_size = alloc_size - 1;
2280db9299fSJens Axboe 		} else {
2290db9299fSJens Axboe 			sg_size = alloc_size;
2300db9299fSJens Axboe 			next = NULL;
2310db9299fSJens Axboe 		}
2320db9299fSJens Axboe 
2330db9299fSJens Axboe 		table->orig_nents -= sg_size;
234c21e59d8STony Battersby 		if (skip_first_chunk)
235c53c6d6aSChristoph Hellwig 			skip_first_chunk = false;
236c21e59d8STony Battersby 		else
237c21e59d8STony Battersby 			free_fn(sgl, alloc_size);
2380db9299fSJens Axboe 		sgl = next;
2390db9299fSJens Axboe 	}
2400db9299fSJens Axboe 
2410db9299fSJens Axboe 	table->sgl = NULL;
2420db9299fSJens Axboe }
2430db9299fSJens Axboe EXPORT_SYMBOL(__sg_free_table);
2440db9299fSJens Axboe 
2450db9299fSJens Axboe /**
2460db9299fSJens Axboe  * sg_free_table - Free a previously allocated sg table
2470db9299fSJens Axboe  * @table:	The mapped sg table header
2480db9299fSJens Axboe  *
2490db9299fSJens Axboe  **/
2500db9299fSJens Axboe void sg_free_table(struct sg_table *table)
2510db9299fSJens Axboe {
252c53c6d6aSChristoph Hellwig 	__sg_free_table(table, SG_MAX_SINGLE_ALLOC, false, sg_kfree);
2530db9299fSJens Axboe }
2540db9299fSJens Axboe EXPORT_SYMBOL(sg_free_table);
2550db9299fSJens Axboe 
2560db9299fSJens Axboe /**
2570db9299fSJens Axboe  * __sg_alloc_table - Allocate and initialize an sg table with given allocator
2580db9299fSJens Axboe  * @table:	The sg table header to use
2590db9299fSJens Axboe  * @nents:	Number of entries in sg list
2607cedb1f1SJames Bottomley  * @max_ents:	The maximum number of entries the allocator returns per call
2610db9299fSJens Axboe  * @gfp_mask:	GFP allocation mask
2620db9299fSJens Axboe  * @alloc_fn:	Allocator to use
2630db9299fSJens Axboe  *
2647cedb1f1SJames Bottomley  * Description:
2657cedb1f1SJames Bottomley  *   This function returns a @table @nents long. The allocator is
2667cedb1f1SJames Bottomley  *   defined to return scatterlist chunks of maximum size @max_ents.
2677cedb1f1SJames Bottomley  *   Thus if @nents is bigger than @max_ents, the scatterlists will be
2687cedb1f1SJames Bottomley  *   chained in units of @max_ents.
2697cedb1f1SJames Bottomley  *
2700db9299fSJens Axboe  * Notes:
2710db9299fSJens Axboe  *   If this function returns non-0 (eg failure), the caller must call
2720db9299fSJens Axboe  *   __sg_free_table() to cleanup any leftover allocations.
2730db9299fSJens Axboe  *
2740db9299fSJens Axboe  **/
2757cedb1f1SJames Bottomley int __sg_alloc_table(struct sg_table *table, unsigned int nents,
276c53c6d6aSChristoph Hellwig 		     unsigned int max_ents, struct scatterlist *first_chunk,
277c53c6d6aSChristoph Hellwig 		     gfp_t gfp_mask, sg_alloc_fn *alloc_fn)
2780db9299fSJens Axboe {
2790db9299fSJens Axboe 	struct scatterlist *sg, *prv;
2800db9299fSJens Axboe 	unsigned int left;
2810db9299fSJens Axboe 
28227daabd9SDan Carpenter 	memset(table, 0, sizeof(*table));
28327daabd9SDan Carpenter 
28427daabd9SDan Carpenter 	if (nents == 0)
28527daabd9SDan Carpenter 		return -EINVAL;
286308c09f1SLaura Abbott #ifndef CONFIG_ARCH_HAS_SG_CHAIN
2876fd59a83SNick Bowler 	if (WARN_ON_ONCE(nents > max_ents))
2886fd59a83SNick Bowler 		return -EINVAL;
2890db9299fSJens Axboe #endif
2900db9299fSJens Axboe 
2910db9299fSJens Axboe 	left = nents;
2920db9299fSJens Axboe 	prv = NULL;
2930db9299fSJens Axboe 	do {
2940db9299fSJens Axboe 		unsigned int sg_size, alloc_size = left;
2950db9299fSJens Axboe 
2967cedb1f1SJames Bottomley 		if (alloc_size > max_ents) {
2977cedb1f1SJames Bottomley 			alloc_size = max_ents;
2980db9299fSJens Axboe 			sg_size = alloc_size - 1;
2990db9299fSJens Axboe 		} else
3000db9299fSJens Axboe 			sg_size = alloc_size;
3010db9299fSJens Axboe 
3020db9299fSJens Axboe 		left -= sg_size;
3030db9299fSJens Axboe 
304c53c6d6aSChristoph Hellwig 		if (first_chunk) {
305c53c6d6aSChristoph Hellwig 			sg = first_chunk;
306c53c6d6aSChristoph Hellwig 			first_chunk = NULL;
307c53c6d6aSChristoph Hellwig 		} else {
3080db9299fSJens Axboe 			sg = alloc_fn(alloc_size, gfp_mask);
309c53c6d6aSChristoph Hellwig 		}
310edce6820SJeffrey Carlyle 		if (unlikely(!sg)) {
311edce6820SJeffrey Carlyle 			/*
312edce6820SJeffrey Carlyle 			 * Adjust entry count to reflect that the last
313edce6820SJeffrey Carlyle 			 * entry of the previous table won't be used for
314edce6820SJeffrey Carlyle 			 * linkage.  Without this, sg_kfree() may get
315edce6820SJeffrey Carlyle 			 * confused.
316edce6820SJeffrey Carlyle 			 */
317edce6820SJeffrey Carlyle 			if (prv)
318edce6820SJeffrey Carlyle 				table->nents = ++table->orig_nents;
319edce6820SJeffrey Carlyle 
3200db9299fSJens Axboe  			return -ENOMEM;
321edce6820SJeffrey Carlyle 		}
3220db9299fSJens Axboe 
3230db9299fSJens Axboe 		sg_init_table(sg, alloc_size);
3240db9299fSJens Axboe 		table->nents = table->orig_nents += sg_size;
3250db9299fSJens Axboe 
3260db9299fSJens Axboe 		/*
3270db9299fSJens Axboe 		 * If this is the first mapping, assign the sg table header.
3280db9299fSJens Axboe 		 * If this is not the first mapping, chain previous part.
3290db9299fSJens Axboe 		 */
3300db9299fSJens Axboe 		if (prv)
3317cedb1f1SJames Bottomley 			sg_chain(prv, max_ents, sg);
3320db9299fSJens Axboe 		else
3330db9299fSJens Axboe 			table->sgl = sg;
3340db9299fSJens Axboe 
3350db9299fSJens Axboe 		/*
3360db9299fSJens Axboe 		 * If no more entries after this one, mark the end
3370db9299fSJens Axboe 		 */
3380db9299fSJens Axboe 		if (!left)
3390db9299fSJens Axboe 			sg_mark_end(&sg[sg_size - 1]);
3400db9299fSJens Axboe 
3410db9299fSJens Axboe 		prv = sg;
3420db9299fSJens Axboe 	} while (left);
3430db9299fSJens Axboe 
3440db9299fSJens Axboe 	return 0;
3450db9299fSJens Axboe }
3460db9299fSJens Axboe EXPORT_SYMBOL(__sg_alloc_table);
3470db9299fSJens Axboe 
3480db9299fSJens Axboe /**
3490db9299fSJens Axboe  * sg_alloc_table - Allocate and initialize an sg table
3500db9299fSJens Axboe  * @table:	The sg table header to use
3510db9299fSJens Axboe  * @nents:	Number of entries in sg list
3520db9299fSJens Axboe  * @gfp_mask:	GFP allocation mask
3530db9299fSJens Axboe  *
3540db9299fSJens Axboe  *  Description:
3550db9299fSJens Axboe  *    Allocate and initialize an sg table. If @nents@ is larger than
3560db9299fSJens Axboe  *    SG_MAX_SINGLE_ALLOC a chained sg table will be setup.
3570db9299fSJens Axboe  *
3580db9299fSJens Axboe  **/
3590db9299fSJens Axboe int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
3600db9299fSJens Axboe {
3610db9299fSJens Axboe 	int ret;
3620db9299fSJens Axboe 
3637cedb1f1SJames Bottomley 	ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
364c53c6d6aSChristoph Hellwig 			       NULL, gfp_mask, sg_kmalloc);
3650db9299fSJens Axboe 	if (unlikely(ret))
366c53c6d6aSChristoph Hellwig 		__sg_free_table(table, SG_MAX_SINGLE_ALLOC, false, sg_kfree);
3670db9299fSJens Axboe 
3680db9299fSJens Axboe 	return ret;
3690db9299fSJens Axboe }
3700db9299fSJens Axboe EXPORT_SYMBOL(sg_alloc_table);
371b1adaf65SFUJITA Tomonori 
372b1adaf65SFUJITA Tomonori /**
37389d8589cSTvrtko Ursulin  * __sg_alloc_table_from_pages - Allocate and initialize an sg table from
374efc42bc9STomasz Stanislawski  *			         an array of pages
375efc42bc9STomasz Stanislawski  * @sgt:	 The sg table header to use
376efc42bc9STomasz Stanislawski  * @pages:	 Pointer to an array of page pointers
377efc42bc9STomasz Stanislawski  * @n_pages:	 Number of pages in the pages array
378efc42bc9STomasz Stanislawski  * @offset:      Offset from start of the first page to the start of a buffer
379efc42bc9STomasz Stanislawski  * @size:        Number of valid bytes in the buffer (after offset)
38089d8589cSTvrtko Ursulin  * @max_segment: Maximum size of a scatterlist node in bytes (page aligned)
381efc42bc9STomasz Stanislawski  * @gfp_mask:	 GFP allocation mask
382efc42bc9STomasz Stanislawski  *
383efc42bc9STomasz Stanislawski  *  Description:
384efc42bc9STomasz Stanislawski  *    Allocate and initialize an sg table from a list of pages. Contiguous
38589d8589cSTvrtko Ursulin  *    ranges of the pages are squashed into a single scatterlist node up to the
38689d8589cSTvrtko Ursulin  *    maximum size specified in @max_segment. An user may provide an offset at a
38789d8589cSTvrtko Ursulin  *    start and a size of valid data in a buffer specified by the page array.
38889d8589cSTvrtko Ursulin  *    The returned sg table is released by sg_free_table.
389efc42bc9STomasz Stanislawski  *
390efc42bc9STomasz Stanislawski  * Returns:
391efc42bc9STomasz Stanislawski  *   0 on success, negative error on failure
392efc42bc9STomasz Stanislawski  */
39389d8589cSTvrtko Ursulin int __sg_alloc_table_from_pages(struct sg_table *sgt, struct page **pages,
39489d8589cSTvrtko Ursulin 				unsigned int n_pages, unsigned int offset,
39589d8589cSTvrtko Ursulin 				unsigned long size, unsigned int max_segment,
396efc42bc9STomasz Stanislawski 				gfp_t gfp_mask)
397efc42bc9STomasz Stanislawski {
398c125906bSTvrtko Ursulin 	unsigned int chunks, cur_page, seg_len, i;
399efc42bc9STomasz Stanislawski 	int ret;
400efc42bc9STomasz Stanislawski 	struct scatterlist *s;
401efc42bc9STomasz Stanislawski 
40289d8589cSTvrtko Ursulin 	if (WARN_ON(!max_segment || offset_in_page(max_segment)))
40389d8589cSTvrtko Ursulin 		return -EINVAL;
40489d8589cSTvrtko Ursulin 
405efc42bc9STomasz Stanislawski 	/* compute number of contiguous chunks */
406efc42bc9STomasz Stanislawski 	chunks = 1;
407c125906bSTvrtko Ursulin 	seg_len = 0;
408c125906bSTvrtko Ursulin 	for (i = 1; i < n_pages; i++) {
409c125906bSTvrtko Ursulin 		seg_len += PAGE_SIZE;
410c125906bSTvrtko Ursulin 		if (seg_len >= max_segment ||
411c125906bSTvrtko Ursulin 		    page_to_pfn(pages[i]) != page_to_pfn(pages[i - 1]) + 1) {
412c125906bSTvrtko Ursulin 			chunks++;
413c125906bSTvrtko Ursulin 			seg_len = 0;
414c125906bSTvrtko Ursulin 		}
415c125906bSTvrtko Ursulin 	}
416efc42bc9STomasz Stanislawski 
417efc42bc9STomasz Stanislawski 	ret = sg_alloc_table(sgt, chunks, gfp_mask);
418efc42bc9STomasz Stanislawski 	if (unlikely(ret))
419efc42bc9STomasz Stanislawski 		return ret;
420efc42bc9STomasz Stanislawski 
421efc42bc9STomasz Stanislawski 	/* merging chunks and putting them into the scatterlist */
422efc42bc9STomasz Stanislawski 	cur_page = 0;
423efc42bc9STomasz Stanislawski 	for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
424c125906bSTvrtko Ursulin 		unsigned int j, chunk_size;
425efc42bc9STomasz Stanislawski 
426efc42bc9STomasz Stanislawski 		/* look for the end of the current chunk */
427c125906bSTvrtko Ursulin 		seg_len = 0;
428c125906bSTvrtko Ursulin 		for (j = cur_page + 1; j < n_pages; j++) {
429c125906bSTvrtko Ursulin 			seg_len += PAGE_SIZE;
430c125906bSTvrtko Ursulin 			if (seg_len >= max_segment ||
431c125906bSTvrtko Ursulin 			    page_to_pfn(pages[j]) !=
432efc42bc9STomasz Stanislawski 			    page_to_pfn(pages[j - 1]) + 1)
433efc42bc9STomasz Stanislawski 				break;
434c125906bSTvrtko Ursulin 		}
435efc42bc9STomasz Stanislawski 
436efc42bc9STomasz Stanislawski 		chunk_size = ((j - cur_page) << PAGE_SHIFT) - offset;
437c125906bSTvrtko Ursulin 		sg_set_page(s, pages[cur_page],
438c125906bSTvrtko Ursulin 			    min_t(unsigned long, size, chunk_size), offset);
439efc42bc9STomasz Stanislawski 		size -= chunk_size;
440efc42bc9STomasz Stanislawski 		offset = 0;
441efc42bc9STomasz Stanislawski 		cur_page = j;
442efc42bc9STomasz Stanislawski 	}
443efc42bc9STomasz Stanislawski 
444efc42bc9STomasz Stanislawski 	return 0;
445efc42bc9STomasz Stanislawski }
44689d8589cSTvrtko Ursulin EXPORT_SYMBOL(__sg_alloc_table_from_pages);
44789d8589cSTvrtko Ursulin 
44889d8589cSTvrtko Ursulin /**
44989d8589cSTvrtko Ursulin  * sg_alloc_table_from_pages - Allocate and initialize an sg table from
45089d8589cSTvrtko Ursulin  *			       an array of pages
45189d8589cSTvrtko Ursulin  * @sgt:	 The sg table header to use
45289d8589cSTvrtko Ursulin  * @pages:	 Pointer to an array of page pointers
45389d8589cSTvrtko Ursulin  * @n_pages:	 Number of pages in the pages array
45489d8589cSTvrtko Ursulin  * @offset:      Offset from start of the first page to the start of a buffer
45589d8589cSTvrtko Ursulin  * @size:        Number of valid bytes in the buffer (after offset)
45689d8589cSTvrtko Ursulin  * @gfp_mask:	 GFP allocation mask
45789d8589cSTvrtko Ursulin  *
45889d8589cSTvrtko Ursulin  *  Description:
45989d8589cSTvrtko Ursulin  *    Allocate and initialize an sg table from a list of pages. Contiguous
46089d8589cSTvrtko Ursulin  *    ranges of the pages are squashed into a single scatterlist node. A user
46189d8589cSTvrtko Ursulin  *    may provide an offset at a start and a size of valid data in a buffer
46289d8589cSTvrtko Ursulin  *    specified by the page array. The returned sg table is released by
46389d8589cSTvrtko Ursulin  *    sg_free_table.
46489d8589cSTvrtko Ursulin  *
46589d8589cSTvrtko Ursulin  * Returns:
46689d8589cSTvrtko Ursulin  *   0 on success, negative error on failure
46789d8589cSTvrtko Ursulin  */
46889d8589cSTvrtko Ursulin int sg_alloc_table_from_pages(struct sg_table *sgt, struct page **pages,
46989d8589cSTvrtko Ursulin 			      unsigned int n_pages, unsigned int offset,
47089d8589cSTvrtko Ursulin 			      unsigned long size, gfp_t gfp_mask)
47189d8589cSTvrtko Ursulin {
47289d8589cSTvrtko Ursulin 	return __sg_alloc_table_from_pages(sgt, pages, n_pages, offset, size,
47389d8589cSTvrtko Ursulin 					   SCATTERLIST_MAX_SEGMENT, gfp_mask);
47489d8589cSTvrtko Ursulin }
475efc42bc9STomasz Stanislawski EXPORT_SYMBOL(sg_alloc_table_from_pages);
476efc42bc9STomasz Stanislawski 
477e80a0af4SBart Van Assche #ifdef CONFIG_SGL_ALLOC
478e80a0af4SBart Van Assche 
479e80a0af4SBart Van Assche /**
480e80a0af4SBart Van Assche  * sgl_alloc_order - allocate a scatterlist and its pages
481e80a0af4SBart Van Assche  * @length: Length in bytes of the scatterlist. Must be at least one
482e80a0af4SBart Van Assche  * @order: Second argument for alloc_pages()
483e80a0af4SBart Van Assche  * @chainable: Whether or not to allocate an extra element in the scatterlist
484e80a0af4SBart Van Assche  *	for scatterlist chaining purposes
485e80a0af4SBart Van Assche  * @gfp: Memory allocation flags
486e80a0af4SBart Van Assche  * @nent_p: [out] Number of entries in the scatterlist that have pages
487e80a0af4SBart Van Assche  *
488e80a0af4SBart Van Assche  * Returns: A pointer to an initialized scatterlist or %NULL upon failure.
489e80a0af4SBart Van Assche  */
490e80a0af4SBart Van Assche struct scatterlist *sgl_alloc_order(unsigned long long length,
491e80a0af4SBart Van Assche 				    unsigned int order, bool chainable,
492e80a0af4SBart Van Assche 				    gfp_t gfp, unsigned int *nent_p)
493e80a0af4SBart Van Assche {
494e80a0af4SBart Van Assche 	struct scatterlist *sgl, *sg;
495e80a0af4SBart Van Assche 	struct page *page;
496e80a0af4SBart Van Assche 	unsigned int nent, nalloc;
497e80a0af4SBart Van Assche 	u32 elem_len;
498e80a0af4SBart Van Assche 
499e80a0af4SBart Van Assche 	nent = round_up(length, PAGE_SIZE << order) >> (PAGE_SHIFT + order);
500e80a0af4SBart Van Assche 	/* Check for integer overflow */
501e80a0af4SBart Van Assche 	if (length > (nent << (PAGE_SHIFT + order)))
502e80a0af4SBart Van Assche 		return NULL;
503e80a0af4SBart Van Assche 	nalloc = nent;
504e80a0af4SBart Van Assche 	if (chainable) {
505e80a0af4SBart Van Assche 		/* Check for integer overflow */
506e80a0af4SBart Van Assche 		if (nalloc + 1 < nalloc)
507e80a0af4SBart Van Assche 			return NULL;
508e80a0af4SBart Van Assche 		nalloc++;
509e80a0af4SBart Van Assche 	}
510e80a0af4SBart Van Assche 	sgl = kmalloc_array(nalloc, sizeof(struct scatterlist),
511e80a0af4SBart Van Assche 			    (gfp & ~GFP_DMA) | __GFP_ZERO);
512e80a0af4SBart Van Assche 	if (!sgl)
513e80a0af4SBart Van Assche 		return NULL;
514e80a0af4SBart Van Assche 
515e80a0af4SBart Van Assche 	sg_init_table(sgl, nent);
516e80a0af4SBart Van Assche 	sg = sgl;
517e80a0af4SBart Van Assche 	while (length) {
518e80a0af4SBart Van Assche 		elem_len = min_t(u64, length, PAGE_SIZE << order);
519e80a0af4SBart Van Assche 		page = alloc_pages(gfp, order);
520e80a0af4SBart Van Assche 		if (!page) {
521e80a0af4SBart Van Assche 			sgl_free(sgl);
522e80a0af4SBart Van Assche 			return NULL;
523e80a0af4SBart Van Assche 		}
524e80a0af4SBart Van Assche 
525e80a0af4SBart Van Assche 		sg_set_page(sg, page, elem_len, 0);
526e80a0af4SBart Van Assche 		length -= elem_len;
527e80a0af4SBart Van Assche 		sg = sg_next(sg);
528e80a0af4SBart Van Assche 	}
529e80a0af4SBart Van Assche 	WARN_ON_ONCE(sg);
530e80a0af4SBart Van Assche 	if (nent_p)
531e80a0af4SBart Van Assche 		*nent_p = nent;
532e80a0af4SBart Van Assche 	return sgl;
533e80a0af4SBart Van Assche }
534e80a0af4SBart Van Assche EXPORT_SYMBOL(sgl_alloc_order);
535e80a0af4SBart Van Assche 
536e80a0af4SBart Van Assche /**
537e80a0af4SBart Van Assche  * sgl_alloc - allocate a scatterlist and its pages
538e80a0af4SBart Van Assche  * @length: Length in bytes of the scatterlist
539e80a0af4SBart Van Assche  * @gfp: Memory allocation flags
540e80a0af4SBart Van Assche  * @nent_p: [out] Number of entries in the scatterlist
541e80a0af4SBart Van Assche  *
542e80a0af4SBart Van Assche  * Returns: A pointer to an initialized scatterlist or %NULL upon failure.
543e80a0af4SBart Van Assche  */
544e80a0af4SBart Van Assche struct scatterlist *sgl_alloc(unsigned long long length, gfp_t gfp,
545e80a0af4SBart Van Assche 			      unsigned int *nent_p)
546e80a0af4SBart Van Assche {
547e80a0af4SBart Van Assche 	return sgl_alloc_order(length, 0, false, gfp, nent_p);
548e80a0af4SBart Van Assche }
549e80a0af4SBart Van Assche EXPORT_SYMBOL(sgl_alloc);
550e80a0af4SBart Van Assche 
551e80a0af4SBart Van Assche /**
552e80a0af4SBart Van Assche  * sgl_free_order - free a scatterlist and its pages
553e80a0af4SBart Van Assche  * @sgl: Scatterlist with one or more elements
554e80a0af4SBart Van Assche  * @order: Second argument for __free_pages()
555e80a0af4SBart Van Assche  */
556e80a0af4SBart Van Assche void sgl_free_order(struct scatterlist *sgl, int order)
557e80a0af4SBart Van Assche {
558e80a0af4SBart Van Assche 	struct scatterlist *sg;
559e80a0af4SBart Van Assche 	struct page *page;
560e80a0af4SBart Van Assche 
561e80a0af4SBart Van Assche 	for (sg = sgl; sg; sg = sg_next(sg)) {
562e80a0af4SBart Van Assche 		page = sg_page(sg);
563e80a0af4SBart Van Assche 		if (page)
564e80a0af4SBart Van Assche 			__free_pages(page, order);
565e80a0af4SBart Van Assche 	}
566e80a0af4SBart Van Assche 	kfree(sgl);
567e80a0af4SBart Van Assche }
568e80a0af4SBart Van Assche EXPORT_SYMBOL(sgl_free_order);
569e80a0af4SBart Van Assche 
570e80a0af4SBart Van Assche /**
571e80a0af4SBart Van Assche  * sgl_free - free a scatterlist and its pages
572e80a0af4SBart Van Assche  * @sgl: Scatterlist with one or more elements
573e80a0af4SBart Van Assche  */
574e80a0af4SBart Van Assche void sgl_free(struct scatterlist *sgl)
575e80a0af4SBart Van Assche {
576e80a0af4SBart Van Assche 	sgl_free_order(sgl, 0);
577e80a0af4SBart Van Assche }
578e80a0af4SBart Van Assche EXPORT_SYMBOL(sgl_free);
579e80a0af4SBart Van Assche 
580e80a0af4SBart Van Assche #endif /* CONFIG_SGL_ALLOC */
581e80a0af4SBart Van Assche 
582a321e91bSImre Deak void __sg_page_iter_start(struct sg_page_iter *piter,
583a321e91bSImre Deak 			  struct scatterlist *sglist, unsigned int nents,
584a321e91bSImre Deak 			  unsigned long pgoffset)
585a321e91bSImre Deak {
586a321e91bSImre Deak 	piter->__pg_advance = 0;
587a321e91bSImre Deak 	piter->__nents = nents;
588a321e91bSImre Deak 
589a321e91bSImre Deak 	piter->sg = sglist;
590a321e91bSImre Deak 	piter->sg_pgoffset = pgoffset;
591a321e91bSImre Deak }
592a321e91bSImre Deak EXPORT_SYMBOL(__sg_page_iter_start);
593a321e91bSImre Deak 
594a321e91bSImre Deak static int sg_page_count(struct scatterlist *sg)
595a321e91bSImre Deak {
596a321e91bSImre Deak 	return PAGE_ALIGN(sg->offset + sg->length) >> PAGE_SHIFT;
597a321e91bSImre Deak }
598a321e91bSImre Deak 
599a321e91bSImre Deak bool __sg_page_iter_next(struct sg_page_iter *piter)
600a321e91bSImre Deak {
601a321e91bSImre Deak 	if (!piter->__nents || !piter->sg)
602a321e91bSImre Deak 		return false;
603a321e91bSImre Deak 
604a321e91bSImre Deak 	piter->sg_pgoffset += piter->__pg_advance;
605a321e91bSImre Deak 	piter->__pg_advance = 1;
606a321e91bSImre Deak 
607a321e91bSImre Deak 	while (piter->sg_pgoffset >= sg_page_count(piter->sg)) {
608a321e91bSImre Deak 		piter->sg_pgoffset -= sg_page_count(piter->sg);
609a321e91bSImre Deak 		piter->sg = sg_next(piter->sg);
610a321e91bSImre Deak 		if (!--piter->__nents || !piter->sg)
611a321e91bSImre Deak 			return false;
612a321e91bSImre Deak 	}
613a321e91bSImre Deak 
614a321e91bSImre Deak 	return true;
615a321e91bSImre Deak }
616a321e91bSImre Deak EXPORT_SYMBOL(__sg_page_iter_next);
617a321e91bSImre Deak 
618efc42bc9STomasz Stanislawski /**
619137d3edbSTejun Heo  * sg_miter_start - start mapping iteration over a sg list
620137d3edbSTejun Heo  * @miter: sg mapping iter to be started
621137d3edbSTejun Heo  * @sgl: sg list to iterate over
622137d3edbSTejun Heo  * @nents: number of sg entries
623137d3edbSTejun Heo  *
624137d3edbSTejun Heo  * Description:
625137d3edbSTejun Heo  *   Starts mapping iterator @miter.
626137d3edbSTejun Heo  *
627137d3edbSTejun Heo  * Context:
628137d3edbSTejun Heo  *   Don't care.
629137d3edbSTejun Heo  */
630137d3edbSTejun Heo void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
631137d3edbSTejun Heo 		    unsigned int nents, unsigned int flags)
632137d3edbSTejun Heo {
633137d3edbSTejun Heo 	memset(miter, 0, sizeof(struct sg_mapping_iter));
634137d3edbSTejun Heo 
6354225fc85SImre Deak 	__sg_page_iter_start(&miter->piter, sgl, nents, 0);
6366de7e356SSebastian Andrzej Siewior 	WARN_ON(!(flags & (SG_MITER_TO_SG | SG_MITER_FROM_SG)));
637137d3edbSTejun Heo 	miter->__flags = flags;
638137d3edbSTejun Heo }
639137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_start);
640137d3edbSTejun Heo 
64111052004SAkinobu Mita static bool sg_miter_get_next_page(struct sg_mapping_iter *miter)
64211052004SAkinobu Mita {
64311052004SAkinobu Mita 	if (!miter->__remaining) {
64411052004SAkinobu Mita 		struct scatterlist *sg;
64511052004SAkinobu Mita 		unsigned long pgoffset;
64611052004SAkinobu Mita 
64711052004SAkinobu Mita 		if (!__sg_page_iter_next(&miter->piter))
64811052004SAkinobu Mita 			return false;
64911052004SAkinobu Mita 
65011052004SAkinobu Mita 		sg = miter->piter.sg;
65111052004SAkinobu Mita 		pgoffset = miter->piter.sg_pgoffset;
65211052004SAkinobu Mita 
65311052004SAkinobu Mita 		miter->__offset = pgoffset ? 0 : sg->offset;
65411052004SAkinobu Mita 		miter->__remaining = sg->offset + sg->length -
65511052004SAkinobu Mita 				(pgoffset << PAGE_SHIFT) - miter->__offset;
65611052004SAkinobu Mita 		miter->__remaining = min_t(unsigned long, miter->__remaining,
65711052004SAkinobu Mita 					   PAGE_SIZE - miter->__offset);
65811052004SAkinobu Mita 	}
65911052004SAkinobu Mita 
66011052004SAkinobu Mita 	return true;
66111052004SAkinobu Mita }
66211052004SAkinobu Mita 
663137d3edbSTejun Heo /**
664df642ceaSAkinobu Mita  * sg_miter_skip - reposition mapping iterator
665df642ceaSAkinobu Mita  * @miter: sg mapping iter to be skipped
666df642ceaSAkinobu Mita  * @offset: number of bytes to plus the current location
667df642ceaSAkinobu Mita  *
668df642ceaSAkinobu Mita  * Description:
669df642ceaSAkinobu Mita  *   Sets the offset of @miter to its current location plus @offset bytes.
670df642ceaSAkinobu Mita  *   If mapping iterator @miter has been proceeded by sg_miter_next(), this
671df642ceaSAkinobu Mita  *   stops @miter.
672df642ceaSAkinobu Mita  *
673df642ceaSAkinobu Mita  * Context:
674df642ceaSAkinobu Mita  *   Don't care if @miter is stopped, or not proceeded yet.
675df642ceaSAkinobu Mita  *   Otherwise, preemption disabled if the SG_MITER_ATOMIC is set.
676df642ceaSAkinobu Mita  *
677df642ceaSAkinobu Mita  * Returns:
678df642ceaSAkinobu Mita  *   true if @miter contains the valid mapping.  false if end of sg
679df642ceaSAkinobu Mita  *   list is reached.
680df642ceaSAkinobu Mita  */
6810d6077f8SMing Lei bool sg_miter_skip(struct sg_mapping_iter *miter, off_t offset)
682df642ceaSAkinobu Mita {
683df642ceaSAkinobu Mita 	sg_miter_stop(miter);
684df642ceaSAkinobu Mita 
685df642ceaSAkinobu Mita 	while (offset) {
686df642ceaSAkinobu Mita 		off_t consumed;
687df642ceaSAkinobu Mita 
688df642ceaSAkinobu Mita 		if (!sg_miter_get_next_page(miter))
689df642ceaSAkinobu Mita 			return false;
690df642ceaSAkinobu Mita 
691df642ceaSAkinobu Mita 		consumed = min_t(off_t, offset, miter->__remaining);
692df642ceaSAkinobu Mita 		miter->__offset += consumed;
693df642ceaSAkinobu Mita 		miter->__remaining -= consumed;
694df642ceaSAkinobu Mita 		offset -= consumed;
695df642ceaSAkinobu Mita 	}
696df642ceaSAkinobu Mita 
697df642ceaSAkinobu Mita 	return true;
698df642ceaSAkinobu Mita }
6990d6077f8SMing Lei EXPORT_SYMBOL(sg_miter_skip);
700df642ceaSAkinobu Mita 
701df642ceaSAkinobu Mita /**
702137d3edbSTejun Heo  * sg_miter_next - proceed mapping iterator to the next mapping
703137d3edbSTejun Heo  * @miter: sg mapping iter to proceed
704137d3edbSTejun Heo  *
705137d3edbSTejun Heo  * Description:
7068290e2d2STejun Heo  *   Proceeds @miter to the next mapping.  @miter should have been started
7078290e2d2STejun Heo  *   using sg_miter_start().  On successful return, @miter->page,
7088290e2d2STejun Heo  *   @miter->addr and @miter->length point to the current mapping.
709137d3edbSTejun Heo  *
710137d3edbSTejun Heo  * Context:
7118290e2d2STejun Heo  *   Preemption disabled if SG_MITER_ATOMIC.  Preemption must stay disabled
7128290e2d2STejun Heo  *   till @miter is stopped.  May sleep if !SG_MITER_ATOMIC.
713137d3edbSTejun Heo  *
714137d3edbSTejun Heo  * Returns:
715137d3edbSTejun Heo  *   true if @miter contains the next mapping.  false if end of sg
716137d3edbSTejun Heo  *   list is reached.
717137d3edbSTejun Heo  */
718137d3edbSTejun Heo bool sg_miter_next(struct sg_mapping_iter *miter)
719137d3edbSTejun Heo {
720137d3edbSTejun Heo 	sg_miter_stop(miter);
721137d3edbSTejun Heo 
7224225fc85SImre Deak 	/*
7234225fc85SImre Deak 	 * Get to the next page if necessary.
7244225fc85SImre Deak 	 * __remaining, __offset is adjusted by sg_miter_stop
7254225fc85SImre Deak 	 */
72611052004SAkinobu Mita 	if (!sg_miter_get_next_page(miter))
72723c560a9STejun Heo 		return false;
7284225fc85SImre Deak 
7292db76d7cSImre Deak 	miter->page = sg_page_iter_page(&miter->piter);
7304225fc85SImre Deak 	miter->consumed = miter->length = miter->__remaining;
731137d3edbSTejun Heo 
732137d3edbSTejun Heo 	if (miter->__flags & SG_MITER_ATOMIC)
7334225fc85SImre Deak 		miter->addr = kmap_atomic(miter->page) + miter->__offset;
734137d3edbSTejun Heo 	else
7354225fc85SImre Deak 		miter->addr = kmap(miter->page) + miter->__offset;
736137d3edbSTejun Heo 
737137d3edbSTejun Heo 	return true;
738137d3edbSTejun Heo }
739137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_next);
740137d3edbSTejun Heo 
741137d3edbSTejun Heo /**
742137d3edbSTejun Heo  * sg_miter_stop - stop mapping iteration
743137d3edbSTejun Heo  * @miter: sg mapping iter to be stopped
744137d3edbSTejun Heo  *
745137d3edbSTejun Heo  * Description:
746137d3edbSTejun Heo  *   Stops mapping iterator @miter.  @miter should have been started
7474ba6a2b2SMasahiro Yamada  *   using sg_miter_start().  A stopped iteration can be resumed by
7484ba6a2b2SMasahiro Yamada  *   calling sg_miter_next() on it.  This is useful when resources (kmap)
7494ba6a2b2SMasahiro Yamada  *   need to be released during iteration.
750137d3edbSTejun Heo  *
751137d3edbSTejun Heo  * Context:
7528290e2d2STejun Heo  *   Preemption disabled if the SG_MITER_ATOMIC is set.  Don't care
7538290e2d2STejun Heo  *   otherwise.
754137d3edbSTejun Heo  */
755137d3edbSTejun Heo void sg_miter_stop(struct sg_mapping_iter *miter)
756137d3edbSTejun Heo {
757137d3edbSTejun Heo 	WARN_ON(miter->consumed > miter->length);
758137d3edbSTejun Heo 
759137d3edbSTejun Heo 	/* drop resources from the last iteration */
760137d3edbSTejun Heo 	if (miter->addr) {
761137d3edbSTejun Heo 		miter->__offset += miter->consumed;
7624225fc85SImre Deak 		miter->__remaining -= miter->consumed;
763137d3edbSTejun Heo 
7643d77b50cSMing Lei 		if ((miter->__flags & SG_MITER_TO_SG) &&
7653d77b50cSMing Lei 		    !PageSlab(miter->page))
7666de7e356SSebastian Andrzej Siewior 			flush_kernel_dcache_page(miter->page);
7676de7e356SSebastian Andrzej Siewior 
768137d3edbSTejun Heo 		if (miter->__flags & SG_MITER_ATOMIC) {
7698290e2d2STejun Heo 			WARN_ON_ONCE(preemptible());
770c3eede8eSCong Wang 			kunmap_atomic(miter->addr);
771137d3edbSTejun Heo 		} else
772f652c521SArjan van de Ven 			kunmap(miter->page);
773137d3edbSTejun Heo 
774137d3edbSTejun Heo 		miter->page = NULL;
775137d3edbSTejun Heo 		miter->addr = NULL;
776137d3edbSTejun Heo 		miter->length = 0;
777137d3edbSTejun Heo 		miter->consumed = 0;
778137d3edbSTejun Heo 	}
779137d3edbSTejun Heo }
780137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_stop);
781137d3edbSTejun Heo 
782137d3edbSTejun Heo /**
783b1adaf65SFUJITA Tomonori  * sg_copy_buffer - Copy data between a linear buffer and an SG list
784b1adaf65SFUJITA Tomonori  * @sgl:		 The SG list
785b1adaf65SFUJITA Tomonori  * @nents:		 Number of SG entries
786b1adaf65SFUJITA Tomonori  * @buf:		 Where to copy from
787b1adaf65SFUJITA Tomonori  * @buflen:		 The number of bytes to copy
788df642ceaSAkinobu Mita  * @skip:		 Number of bytes to skip before copying
789df642ceaSAkinobu Mita  * @to_buffer:		 transfer direction (true == from an sg list to a
790df642ceaSAkinobu Mita  *			 buffer, false == from a buffer to an sg list
791b1adaf65SFUJITA Tomonori  *
792b1adaf65SFUJITA Tomonori  * Returns the number of copied bytes.
793b1adaf65SFUJITA Tomonori  *
794b1adaf65SFUJITA Tomonori  **/
795386ecb12SDave Gordon size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf,
796386ecb12SDave Gordon 		      size_t buflen, off_t skip, bool to_buffer)
797b1adaf65SFUJITA Tomonori {
798137d3edbSTejun Heo 	unsigned int offset = 0;
799137d3edbSTejun Heo 	struct sg_mapping_iter miter;
8006de7e356SSebastian Andrzej Siewior 	unsigned int sg_flags = SG_MITER_ATOMIC;
801b1adaf65SFUJITA Tomonori 
8026de7e356SSebastian Andrzej Siewior 	if (to_buffer)
8036de7e356SSebastian Andrzej Siewior 		sg_flags |= SG_MITER_FROM_SG;
8046de7e356SSebastian Andrzej Siewior 	else
8056de7e356SSebastian Andrzej Siewior 		sg_flags |= SG_MITER_TO_SG;
8066de7e356SSebastian Andrzej Siewior 
8076de7e356SSebastian Andrzej Siewior 	sg_miter_start(&miter, sgl, nents, sg_flags);
808b1adaf65SFUJITA Tomonori 
809df642ceaSAkinobu Mita 	if (!sg_miter_skip(&miter, skip))
810df642ceaSAkinobu Mita 		return false;
811df642ceaSAkinobu Mita 
8121d5210efSGilad Ben-Yossef 	while ((offset < buflen) && sg_miter_next(&miter)) {
813137d3edbSTejun Heo 		unsigned int len;
814b1adaf65SFUJITA Tomonori 
815137d3edbSTejun Heo 		len = min(miter.length, buflen - offset);
816b1adaf65SFUJITA Tomonori 
817b1adaf65SFUJITA Tomonori 		if (to_buffer)
818137d3edbSTejun Heo 			memcpy(buf + offset, miter.addr, len);
8196de7e356SSebastian Andrzej Siewior 		else
820137d3edbSTejun Heo 			memcpy(miter.addr, buf + offset, len);
821b1adaf65SFUJITA Tomonori 
822137d3edbSTejun Heo 		offset += len;
823b1adaf65SFUJITA Tomonori 	}
824b1adaf65SFUJITA Tomonori 
825137d3edbSTejun Heo 	sg_miter_stop(&miter);
826b1adaf65SFUJITA Tomonori 
827137d3edbSTejun Heo 	return offset;
828b1adaf65SFUJITA Tomonori }
829386ecb12SDave Gordon EXPORT_SYMBOL(sg_copy_buffer);
830b1adaf65SFUJITA Tomonori 
831b1adaf65SFUJITA Tomonori /**
832b1adaf65SFUJITA Tomonori  * sg_copy_from_buffer - Copy from a linear buffer to an SG list
833b1adaf65SFUJITA Tomonori  * @sgl:		 The SG list
834b1adaf65SFUJITA Tomonori  * @nents:		 Number of SG entries
835b1adaf65SFUJITA Tomonori  * @buf:		 Where to copy from
836b1adaf65SFUJITA Tomonori  * @buflen:		 The number of bytes to copy
837b1adaf65SFUJITA Tomonori  *
838b1adaf65SFUJITA Tomonori  * Returns the number of copied bytes.
839b1adaf65SFUJITA Tomonori  *
840b1adaf65SFUJITA Tomonori  **/
841b1adaf65SFUJITA Tomonori size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
8422a1bf8f9SDave Gordon 			   const void *buf, size_t buflen)
843b1adaf65SFUJITA Tomonori {
8442a1bf8f9SDave Gordon 	return sg_copy_buffer(sgl, nents, (void *)buf, buflen, 0, false);
845b1adaf65SFUJITA Tomonori }
846b1adaf65SFUJITA Tomonori EXPORT_SYMBOL(sg_copy_from_buffer);
847b1adaf65SFUJITA Tomonori 
848b1adaf65SFUJITA Tomonori /**
849b1adaf65SFUJITA Tomonori  * sg_copy_to_buffer - Copy from an SG list to a linear buffer
850b1adaf65SFUJITA Tomonori  * @sgl:		 The SG list
851b1adaf65SFUJITA Tomonori  * @nents:		 Number of SG entries
852b1adaf65SFUJITA Tomonori  * @buf:		 Where to copy to
853b1adaf65SFUJITA Tomonori  * @buflen:		 The number of bytes to copy
854b1adaf65SFUJITA Tomonori  *
855b1adaf65SFUJITA Tomonori  * Returns the number of copied bytes.
856b1adaf65SFUJITA Tomonori  *
857b1adaf65SFUJITA Tomonori  **/
858b1adaf65SFUJITA Tomonori size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
859b1adaf65SFUJITA Tomonori 			 void *buf, size_t buflen)
860b1adaf65SFUJITA Tomonori {
861df642ceaSAkinobu Mita 	return sg_copy_buffer(sgl, nents, buf, buflen, 0, true);
862b1adaf65SFUJITA Tomonori }
863b1adaf65SFUJITA Tomonori EXPORT_SYMBOL(sg_copy_to_buffer);
864df642ceaSAkinobu Mita 
865df642ceaSAkinobu Mita /**
866df642ceaSAkinobu Mita  * sg_pcopy_from_buffer - Copy from a linear buffer to an SG list
867df642ceaSAkinobu Mita  * @sgl:		 The SG list
868df642ceaSAkinobu Mita  * @nents:		 Number of SG entries
869df642ceaSAkinobu Mita  * @buf:		 Where to copy from
870df642ceaSAkinobu Mita  * @buflen:		 The number of bytes to copy
8714dc7daf8SDave Gordon  * @skip:		 Number of bytes to skip before copying
872df642ceaSAkinobu Mita  *
873df642ceaSAkinobu Mita  * Returns the number of copied bytes.
874df642ceaSAkinobu Mita  *
875df642ceaSAkinobu Mita  **/
876df642ceaSAkinobu Mita size_t sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents,
8772a1bf8f9SDave Gordon 			    const void *buf, size_t buflen, off_t skip)
878df642ceaSAkinobu Mita {
8792a1bf8f9SDave Gordon 	return sg_copy_buffer(sgl, nents, (void *)buf, buflen, skip, false);
880df642ceaSAkinobu Mita }
881df642ceaSAkinobu Mita EXPORT_SYMBOL(sg_pcopy_from_buffer);
882df642ceaSAkinobu Mita 
883df642ceaSAkinobu Mita /**
884df642ceaSAkinobu Mita  * sg_pcopy_to_buffer - Copy from an SG list to a linear buffer
885df642ceaSAkinobu Mita  * @sgl:		 The SG list
886df642ceaSAkinobu Mita  * @nents:		 Number of SG entries
887df642ceaSAkinobu Mita  * @buf:		 Where to copy to
888df642ceaSAkinobu Mita  * @buflen:		 The number of bytes to copy
8894dc7daf8SDave Gordon  * @skip:		 Number of bytes to skip before copying
890df642ceaSAkinobu Mita  *
891df642ceaSAkinobu Mita  * Returns the number of copied bytes.
892df642ceaSAkinobu Mita  *
893df642ceaSAkinobu Mita  **/
894df642ceaSAkinobu Mita size_t sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents,
895df642ceaSAkinobu Mita 			  void *buf, size_t buflen, off_t skip)
896df642ceaSAkinobu Mita {
897df642ceaSAkinobu Mita 	return sg_copy_buffer(sgl, nents, buf, buflen, skip, true);
898df642ceaSAkinobu Mita }
899df642ceaSAkinobu Mita EXPORT_SYMBOL(sg_pcopy_to_buffer);
9000945e569SJohannes Thumshirn 
9010945e569SJohannes Thumshirn /**
9020945e569SJohannes Thumshirn  * sg_zero_buffer - Zero-out a part of a SG list
9030945e569SJohannes Thumshirn  * @sgl:		 The SG list
9040945e569SJohannes Thumshirn  * @nents:		 Number of SG entries
9050945e569SJohannes Thumshirn  * @buflen:		 The number of bytes to zero out
9060945e569SJohannes Thumshirn  * @skip:		 Number of bytes to skip before zeroing
9070945e569SJohannes Thumshirn  *
9080945e569SJohannes Thumshirn  * Returns the number of bytes zeroed.
9090945e569SJohannes Thumshirn  **/
9100945e569SJohannes Thumshirn size_t sg_zero_buffer(struct scatterlist *sgl, unsigned int nents,
9110945e569SJohannes Thumshirn 		       size_t buflen, off_t skip)
9120945e569SJohannes Thumshirn {
9130945e569SJohannes Thumshirn 	unsigned int offset = 0;
9140945e569SJohannes Thumshirn 	struct sg_mapping_iter miter;
9150945e569SJohannes Thumshirn 	unsigned int sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG;
9160945e569SJohannes Thumshirn 
9170945e569SJohannes Thumshirn 	sg_miter_start(&miter, sgl, nents, sg_flags);
9180945e569SJohannes Thumshirn 
9190945e569SJohannes Thumshirn 	if (!sg_miter_skip(&miter, skip))
9200945e569SJohannes Thumshirn 		return false;
9210945e569SJohannes Thumshirn 
9220945e569SJohannes Thumshirn 	while (offset < buflen && sg_miter_next(&miter)) {
9230945e569SJohannes Thumshirn 		unsigned int len;
9240945e569SJohannes Thumshirn 
9250945e569SJohannes Thumshirn 		len = min(miter.length, buflen - offset);
9260945e569SJohannes Thumshirn 		memset(miter.addr, 0, len);
9270945e569SJohannes Thumshirn 
9280945e569SJohannes Thumshirn 		offset += len;
9290945e569SJohannes Thumshirn 	}
9300945e569SJohannes Thumshirn 
9310945e569SJohannes Thumshirn 	sg_miter_stop(&miter);
9320945e569SJohannes Thumshirn 	return offset;
9330945e569SJohannes Thumshirn }
9340945e569SJohannes Thumshirn EXPORT_SYMBOL(sg_zero_buffer);
935