xref: /openbmc/linux/lib/scatterlist.c (revision 137d3edb)
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>
100db9299fSJens Axboe #include <linux/scatterlist.h>
11b1adaf65SFUJITA Tomonori #include <linux/highmem.h>
120db9299fSJens Axboe 
130db9299fSJens Axboe /**
140db9299fSJens Axboe  * sg_next - return the next scatterlist entry in a list
150db9299fSJens Axboe  * @sg:		The current sg entry
160db9299fSJens Axboe  *
170db9299fSJens Axboe  * Description:
180db9299fSJens Axboe  *   Usually the next entry will be @sg@ + 1, but if this sg element is part
190db9299fSJens Axboe  *   of a chained scatterlist, it could jump to the start of a new
200db9299fSJens Axboe  *   scatterlist array.
210db9299fSJens Axboe  *
220db9299fSJens Axboe  **/
230db9299fSJens Axboe struct scatterlist *sg_next(struct scatterlist *sg)
240db9299fSJens Axboe {
250db9299fSJens Axboe #ifdef CONFIG_DEBUG_SG
260db9299fSJens Axboe 	BUG_ON(sg->sg_magic != SG_MAGIC);
270db9299fSJens Axboe #endif
280db9299fSJens Axboe 	if (sg_is_last(sg))
290db9299fSJens Axboe 		return NULL;
300db9299fSJens Axboe 
310db9299fSJens Axboe 	sg++;
320db9299fSJens Axboe 	if (unlikely(sg_is_chain(sg)))
330db9299fSJens Axboe 		sg = sg_chain_ptr(sg);
340db9299fSJens Axboe 
350db9299fSJens Axboe 	return sg;
360db9299fSJens Axboe }
370db9299fSJens Axboe EXPORT_SYMBOL(sg_next);
380db9299fSJens Axboe 
390db9299fSJens Axboe /**
400db9299fSJens Axboe  * sg_last - return the last scatterlist entry in a list
410db9299fSJens Axboe  * @sgl:	First entry in the scatterlist
420db9299fSJens Axboe  * @nents:	Number of entries in the scatterlist
430db9299fSJens Axboe  *
440db9299fSJens Axboe  * Description:
450db9299fSJens Axboe  *   Should only be used casually, it (currently) scans the entire list
460db9299fSJens Axboe  *   to get the last entry.
470db9299fSJens Axboe  *
480db9299fSJens Axboe  *   Note that the @sgl@ pointer passed in need not be the first one,
490db9299fSJens Axboe  *   the important bit is that @nents@ denotes the number of entries that
500db9299fSJens Axboe  *   exist from @sgl@.
510db9299fSJens Axboe  *
520db9299fSJens Axboe  **/
530db9299fSJens Axboe struct scatterlist *sg_last(struct scatterlist *sgl, unsigned int nents)
540db9299fSJens Axboe {
550db9299fSJens Axboe #ifndef ARCH_HAS_SG_CHAIN
560db9299fSJens Axboe 	struct scatterlist *ret = &sgl[nents - 1];
570db9299fSJens Axboe #else
580db9299fSJens Axboe 	struct scatterlist *sg, *ret = NULL;
590db9299fSJens Axboe 	unsigned int i;
600db9299fSJens Axboe 
610db9299fSJens Axboe 	for_each_sg(sgl, sg, nents, i)
620db9299fSJens Axboe 		ret = sg;
630db9299fSJens Axboe 
640db9299fSJens Axboe #endif
650db9299fSJens Axboe #ifdef CONFIG_DEBUG_SG
660db9299fSJens Axboe 	BUG_ON(sgl[0].sg_magic != SG_MAGIC);
670db9299fSJens Axboe 	BUG_ON(!sg_is_last(ret));
680db9299fSJens Axboe #endif
690db9299fSJens Axboe 	return ret;
700db9299fSJens Axboe }
710db9299fSJens Axboe EXPORT_SYMBOL(sg_last);
720db9299fSJens Axboe 
730db9299fSJens Axboe /**
740db9299fSJens Axboe  * sg_init_table - Initialize SG table
750db9299fSJens Axboe  * @sgl:	   The SG table
760db9299fSJens Axboe  * @nents:	   Number of entries in table
770db9299fSJens Axboe  *
780db9299fSJens Axboe  * Notes:
790db9299fSJens Axboe  *   If this is part of a chained sg table, sg_mark_end() should be
800db9299fSJens Axboe  *   used only on the last table part.
810db9299fSJens Axboe  *
820db9299fSJens Axboe  **/
830db9299fSJens Axboe void sg_init_table(struct scatterlist *sgl, unsigned int nents)
840db9299fSJens Axboe {
850db9299fSJens Axboe 	memset(sgl, 0, sizeof(*sgl) * nents);
860db9299fSJens Axboe #ifdef CONFIG_DEBUG_SG
870db9299fSJens Axboe 	{
880db9299fSJens Axboe 		unsigned int i;
890db9299fSJens Axboe 		for (i = 0; i < nents; i++)
900db9299fSJens Axboe 			sgl[i].sg_magic = SG_MAGIC;
910db9299fSJens Axboe 	}
920db9299fSJens Axboe #endif
930db9299fSJens Axboe 	sg_mark_end(&sgl[nents - 1]);
940db9299fSJens Axboe }
950db9299fSJens Axboe EXPORT_SYMBOL(sg_init_table);
960db9299fSJens Axboe 
970db9299fSJens Axboe /**
980db9299fSJens Axboe  * sg_init_one - Initialize a single entry sg list
990db9299fSJens Axboe  * @sg:		 SG entry
1000db9299fSJens Axboe  * @buf:	 Virtual address for IO
1010db9299fSJens Axboe  * @buflen:	 IO length
1020db9299fSJens Axboe  *
1030db9299fSJens Axboe  **/
1040db9299fSJens Axboe void sg_init_one(struct scatterlist *sg, const void *buf, unsigned int buflen)
1050db9299fSJens Axboe {
1060db9299fSJens Axboe 	sg_init_table(sg, 1);
1070db9299fSJens Axboe 	sg_set_buf(sg, buf, buflen);
1080db9299fSJens Axboe }
1090db9299fSJens Axboe EXPORT_SYMBOL(sg_init_one);
1100db9299fSJens Axboe 
1110db9299fSJens Axboe /*
1120db9299fSJens Axboe  * The default behaviour of sg_alloc_table() is to use these kmalloc/kfree
1130db9299fSJens Axboe  * helpers.
1140db9299fSJens Axboe  */
1150db9299fSJens Axboe static struct scatterlist *sg_kmalloc(unsigned int nents, gfp_t gfp_mask)
1160db9299fSJens Axboe {
1170db9299fSJens Axboe 	if (nents == SG_MAX_SINGLE_ALLOC)
1180db9299fSJens Axboe 		return (struct scatterlist *) __get_free_page(gfp_mask);
1190db9299fSJens Axboe 	else
1200db9299fSJens Axboe 		return kmalloc(nents * sizeof(struct scatterlist), gfp_mask);
1210db9299fSJens Axboe }
1220db9299fSJens Axboe 
1230db9299fSJens Axboe static void sg_kfree(struct scatterlist *sg, unsigned int nents)
1240db9299fSJens Axboe {
1250db9299fSJens Axboe 	if (nents == SG_MAX_SINGLE_ALLOC)
1260db9299fSJens Axboe 		free_page((unsigned long) sg);
1270db9299fSJens Axboe 	else
1280db9299fSJens Axboe 		kfree(sg);
1290db9299fSJens Axboe }
1300db9299fSJens Axboe 
1310db9299fSJens Axboe /**
1320db9299fSJens Axboe  * __sg_free_table - Free a previously mapped sg table
1330db9299fSJens Axboe  * @table:	The sg table header to use
1347cedb1f1SJames Bottomley  * @max_ents:	The maximum number of entries per single scatterlist
1350db9299fSJens Axboe  * @free_fn:	Free function
1360db9299fSJens Axboe  *
1370db9299fSJens Axboe  *  Description:
1387cedb1f1SJames Bottomley  *    Free an sg table previously allocated and setup with
1397cedb1f1SJames Bottomley  *    __sg_alloc_table().  The @max_ents value must be identical to
1407cedb1f1SJames Bottomley  *    that previously used with __sg_alloc_table().
1410db9299fSJens Axboe  *
1420db9299fSJens Axboe  **/
1437cedb1f1SJames Bottomley void __sg_free_table(struct sg_table *table, unsigned int max_ents,
1447cedb1f1SJames Bottomley 		     sg_free_fn *free_fn)
1450db9299fSJens Axboe {
1460db9299fSJens Axboe 	struct scatterlist *sgl, *next;
1470db9299fSJens Axboe 
1480db9299fSJens Axboe 	if (unlikely(!table->sgl))
1490db9299fSJens Axboe 		return;
1500db9299fSJens Axboe 
1510db9299fSJens Axboe 	sgl = table->sgl;
1520db9299fSJens Axboe 	while (table->orig_nents) {
1530db9299fSJens Axboe 		unsigned int alloc_size = table->orig_nents;
1540db9299fSJens Axboe 		unsigned int sg_size;
1550db9299fSJens Axboe 
1560db9299fSJens Axboe 		/*
1577cedb1f1SJames Bottomley 		 * If we have more than max_ents segments left,
1580db9299fSJens Axboe 		 * then assign 'next' to the sg table after the current one.
1590db9299fSJens Axboe 		 * sg_size is then one less than alloc size, since the last
1600db9299fSJens Axboe 		 * element is the chain pointer.
1610db9299fSJens Axboe 		 */
1627cedb1f1SJames Bottomley 		if (alloc_size > max_ents) {
1637cedb1f1SJames Bottomley 			next = sg_chain_ptr(&sgl[max_ents - 1]);
1647cedb1f1SJames Bottomley 			alloc_size = max_ents;
1650db9299fSJens Axboe 			sg_size = alloc_size - 1;
1660db9299fSJens Axboe 		} else {
1670db9299fSJens Axboe 			sg_size = alloc_size;
1680db9299fSJens Axboe 			next = NULL;
1690db9299fSJens Axboe 		}
1700db9299fSJens Axboe 
1710db9299fSJens Axboe 		table->orig_nents -= sg_size;
1720db9299fSJens Axboe 		free_fn(sgl, alloc_size);
1730db9299fSJens Axboe 		sgl = next;
1740db9299fSJens Axboe 	}
1750db9299fSJens Axboe 
1760db9299fSJens Axboe 	table->sgl = NULL;
1770db9299fSJens Axboe }
1780db9299fSJens Axboe EXPORT_SYMBOL(__sg_free_table);
1790db9299fSJens Axboe 
1800db9299fSJens Axboe /**
1810db9299fSJens Axboe  * sg_free_table - Free a previously allocated sg table
1820db9299fSJens Axboe  * @table:	The mapped sg table header
1830db9299fSJens Axboe  *
1840db9299fSJens Axboe  **/
1850db9299fSJens Axboe void sg_free_table(struct sg_table *table)
1860db9299fSJens Axboe {
1877cedb1f1SJames Bottomley 	__sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree);
1880db9299fSJens Axboe }
1890db9299fSJens Axboe EXPORT_SYMBOL(sg_free_table);
1900db9299fSJens Axboe 
1910db9299fSJens Axboe /**
1920db9299fSJens Axboe  * __sg_alloc_table - Allocate and initialize an sg table with given allocator
1930db9299fSJens Axboe  * @table:	The sg table header to use
1940db9299fSJens Axboe  * @nents:	Number of entries in sg list
1957cedb1f1SJames Bottomley  * @max_ents:	The maximum number of entries the allocator returns per call
1960db9299fSJens Axboe  * @gfp_mask:	GFP allocation mask
1970db9299fSJens Axboe  * @alloc_fn:	Allocator to use
1980db9299fSJens Axboe  *
1997cedb1f1SJames Bottomley  * Description:
2007cedb1f1SJames Bottomley  *   This function returns a @table @nents long. The allocator is
2017cedb1f1SJames Bottomley  *   defined to return scatterlist chunks of maximum size @max_ents.
2027cedb1f1SJames Bottomley  *   Thus if @nents is bigger than @max_ents, the scatterlists will be
2037cedb1f1SJames Bottomley  *   chained in units of @max_ents.
2047cedb1f1SJames Bottomley  *
2050db9299fSJens Axboe  * Notes:
2060db9299fSJens Axboe  *   If this function returns non-0 (eg failure), the caller must call
2070db9299fSJens Axboe  *   __sg_free_table() to cleanup any leftover allocations.
2080db9299fSJens Axboe  *
2090db9299fSJens Axboe  **/
2107cedb1f1SJames Bottomley int __sg_alloc_table(struct sg_table *table, unsigned int nents,
2117cedb1f1SJames Bottomley 		     unsigned int max_ents, gfp_t gfp_mask,
2120db9299fSJens Axboe 		     sg_alloc_fn *alloc_fn)
2130db9299fSJens Axboe {
2140db9299fSJens Axboe 	struct scatterlist *sg, *prv;
2150db9299fSJens Axboe 	unsigned int left;
2160db9299fSJens Axboe 
2170db9299fSJens Axboe #ifndef ARCH_HAS_SG_CHAIN
2187cedb1f1SJames Bottomley 	BUG_ON(nents > max_ents);
2190db9299fSJens Axboe #endif
2200db9299fSJens Axboe 
2210db9299fSJens Axboe 	memset(table, 0, sizeof(*table));
2220db9299fSJens Axboe 
2230db9299fSJens Axboe 	left = nents;
2240db9299fSJens Axboe 	prv = NULL;
2250db9299fSJens Axboe 	do {
2260db9299fSJens Axboe 		unsigned int sg_size, alloc_size = left;
2270db9299fSJens Axboe 
2287cedb1f1SJames Bottomley 		if (alloc_size > max_ents) {
2297cedb1f1SJames Bottomley 			alloc_size = max_ents;
2300db9299fSJens Axboe 			sg_size = alloc_size - 1;
2310db9299fSJens Axboe 		} else
2320db9299fSJens Axboe 			sg_size = alloc_size;
2330db9299fSJens Axboe 
2340db9299fSJens Axboe 		left -= sg_size;
2350db9299fSJens Axboe 
2360db9299fSJens Axboe 		sg = alloc_fn(alloc_size, gfp_mask);
2370db9299fSJens Axboe 		if (unlikely(!sg))
2380db9299fSJens Axboe 			return -ENOMEM;
2390db9299fSJens Axboe 
2400db9299fSJens Axboe 		sg_init_table(sg, alloc_size);
2410db9299fSJens Axboe 		table->nents = table->orig_nents += sg_size;
2420db9299fSJens Axboe 
2430db9299fSJens Axboe 		/*
2440db9299fSJens Axboe 		 * If this is the first mapping, assign the sg table header.
2450db9299fSJens Axboe 		 * If this is not the first mapping, chain previous part.
2460db9299fSJens Axboe 		 */
2470db9299fSJens Axboe 		if (prv)
2487cedb1f1SJames Bottomley 			sg_chain(prv, max_ents, sg);
2490db9299fSJens Axboe 		else
2500db9299fSJens Axboe 			table->sgl = sg;
2510db9299fSJens Axboe 
2520db9299fSJens Axboe 		/*
2530db9299fSJens Axboe 		 * If no more entries after this one, mark the end
2540db9299fSJens Axboe 		 */
2550db9299fSJens Axboe 		if (!left)
2560db9299fSJens Axboe 			sg_mark_end(&sg[sg_size - 1]);
2570db9299fSJens Axboe 
2580db9299fSJens Axboe 		/*
2590db9299fSJens Axboe 		 * only really needed for mempool backed sg allocations (like
2600db9299fSJens Axboe 		 * SCSI), a possible improvement here would be to pass the
2610db9299fSJens Axboe 		 * table pointer into the allocator and let that clear these
2620db9299fSJens Axboe 		 * flags
2630db9299fSJens Axboe 		 */
2640db9299fSJens Axboe 		gfp_mask &= ~__GFP_WAIT;
2650db9299fSJens Axboe 		gfp_mask |= __GFP_HIGH;
2660db9299fSJens Axboe 		prv = sg;
2670db9299fSJens Axboe 	} while (left);
2680db9299fSJens Axboe 
2690db9299fSJens Axboe 	return 0;
2700db9299fSJens Axboe }
2710db9299fSJens Axboe EXPORT_SYMBOL(__sg_alloc_table);
2720db9299fSJens Axboe 
2730db9299fSJens Axboe /**
2740db9299fSJens Axboe  * sg_alloc_table - Allocate and initialize an sg table
2750db9299fSJens Axboe  * @table:	The sg table header to use
2760db9299fSJens Axboe  * @nents:	Number of entries in sg list
2770db9299fSJens Axboe  * @gfp_mask:	GFP allocation mask
2780db9299fSJens Axboe  *
2790db9299fSJens Axboe  *  Description:
2800db9299fSJens Axboe  *    Allocate and initialize an sg table. If @nents@ is larger than
2810db9299fSJens Axboe  *    SG_MAX_SINGLE_ALLOC a chained sg table will be setup.
2820db9299fSJens Axboe  *
2830db9299fSJens Axboe  **/
2840db9299fSJens Axboe int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
2850db9299fSJens Axboe {
2860db9299fSJens Axboe 	int ret;
2870db9299fSJens Axboe 
2887cedb1f1SJames Bottomley 	ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
2897cedb1f1SJames Bottomley 			       gfp_mask, sg_kmalloc);
2900db9299fSJens Axboe 	if (unlikely(ret))
2917cedb1f1SJames Bottomley 		__sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree);
2920db9299fSJens Axboe 
2930db9299fSJens Axboe 	return ret;
2940db9299fSJens Axboe }
2950db9299fSJens Axboe EXPORT_SYMBOL(sg_alloc_table);
296b1adaf65SFUJITA Tomonori 
297b1adaf65SFUJITA Tomonori /**
298137d3edbSTejun Heo  * sg_miter_start - start mapping iteration over a sg list
299137d3edbSTejun Heo  * @miter: sg mapping iter to be started
300137d3edbSTejun Heo  * @sgl: sg list to iterate over
301137d3edbSTejun Heo  * @nents: number of sg entries
302137d3edbSTejun Heo  *
303137d3edbSTejun Heo  * Description:
304137d3edbSTejun Heo  *   Starts mapping iterator @miter.
305137d3edbSTejun Heo  *
306137d3edbSTejun Heo  * Context:
307137d3edbSTejun Heo  *   Don't care.
308137d3edbSTejun Heo  */
309137d3edbSTejun Heo void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
310137d3edbSTejun Heo 		    unsigned int nents, unsigned int flags)
311137d3edbSTejun Heo {
312137d3edbSTejun Heo 	memset(miter, 0, sizeof(struct sg_mapping_iter));
313137d3edbSTejun Heo 
314137d3edbSTejun Heo 	miter->__sg = sgl;
315137d3edbSTejun Heo 	miter->__nents = nents;
316137d3edbSTejun Heo 	miter->__offset = 0;
317137d3edbSTejun Heo 	miter->__flags = flags;
318137d3edbSTejun Heo }
319137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_start);
320137d3edbSTejun Heo 
321137d3edbSTejun Heo /**
322137d3edbSTejun Heo  * sg_miter_next - proceed mapping iterator to the next mapping
323137d3edbSTejun Heo  * @miter: sg mapping iter to proceed
324137d3edbSTejun Heo  *
325137d3edbSTejun Heo  * Description:
326137d3edbSTejun Heo  *   Proceeds @miter@ to the next mapping.  @miter@ should have been
327137d3edbSTejun Heo  *   started using sg_miter_start().  On successful return,
328137d3edbSTejun Heo  *   @miter@->page, @miter@->addr and @miter@->length point to the
329137d3edbSTejun Heo  *   current mapping.
330137d3edbSTejun Heo  *
331137d3edbSTejun Heo  * Context:
332137d3edbSTejun Heo  *   IRQ disabled if SG_MITER_ATOMIC.  IRQ must stay disabled till
333137d3edbSTejun Heo  *   @miter@ is stopped.  May sleep if !SG_MITER_ATOMIC.
334137d3edbSTejun Heo  *
335137d3edbSTejun Heo  * Returns:
336137d3edbSTejun Heo  *   true if @miter contains the next mapping.  false if end of sg
337137d3edbSTejun Heo  *   list is reached.
338137d3edbSTejun Heo  */
339137d3edbSTejun Heo bool sg_miter_next(struct sg_mapping_iter *miter)
340137d3edbSTejun Heo {
341137d3edbSTejun Heo 	unsigned int off, len;
342137d3edbSTejun Heo 
343137d3edbSTejun Heo 	/* check for end and drop resources from the last iteration */
344137d3edbSTejun Heo 	if (!miter->__nents)
345137d3edbSTejun Heo 		return false;
346137d3edbSTejun Heo 
347137d3edbSTejun Heo 	sg_miter_stop(miter);
348137d3edbSTejun Heo 
349137d3edbSTejun Heo 	/* get to the next sg if necessary.  __offset is adjusted by stop */
350137d3edbSTejun Heo 	if (miter->__offset == miter->__sg->length && --miter->__nents) {
351137d3edbSTejun Heo 		miter->__sg = sg_next(miter->__sg);
352137d3edbSTejun Heo 		miter->__offset = 0;
353137d3edbSTejun Heo 	}
354137d3edbSTejun Heo 
355137d3edbSTejun Heo 	/* map the next page */
356137d3edbSTejun Heo 	off = miter->__sg->offset + miter->__offset;
357137d3edbSTejun Heo 	len = miter->__sg->length - miter->__offset;
358137d3edbSTejun Heo 
359137d3edbSTejun Heo 	miter->page = nth_page(sg_page(miter->__sg), off >> PAGE_SHIFT);
360137d3edbSTejun Heo 	off &= ~PAGE_MASK;
361137d3edbSTejun Heo 	miter->length = min_t(unsigned int, len, PAGE_SIZE - off);
362137d3edbSTejun Heo 	miter->consumed = miter->length;
363137d3edbSTejun Heo 
364137d3edbSTejun Heo 	if (miter->__flags & SG_MITER_ATOMIC)
365137d3edbSTejun Heo 		miter->addr = kmap_atomic(miter->page, KM_BIO_SRC_IRQ) + off;
366137d3edbSTejun Heo 	else
367137d3edbSTejun Heo 		miter->addr = kmap(miter->page) + off;
368137d3edbSTejun Heo 
369137d3edbSTejun Heo 	return true;
370137d3edbSTejun Heo }
371137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_next);
372137d3edbSTejun Heo 
373137d3edbSTejun Heo /**
374137d3edbSTejun Heo  * sg_miter_stop - stop mapping iteration
375137d3edbSTejun Heo  * @miter: sg mapping iter to be stopped
376137d3edbSTejun Heo  *
377137d3edbSTejun Heo  * Description:
378137d3edbSTejun Heo  *   Stops mapping iterator @miter.  @miter should have been started
379137d3edbSTejun Heo  *   started using sg_miter_start().  A stopped iteration can be
380137d3edbSTejun Heo  *   resumed by calling sg_miter_next() on it.  This is useful when
381137d3edbSTejun Heo  *   resources (kmap) need to be released during iteration.
382137d3edbSTejun Heo  *
383137d3edbSTejun Heo  * Context:
384137d3edbSTejun Heo  *   IRQ disabled if the SG_MITER_ATOMIC is set.  Don't care otherwise.
385137d3edbSTejun Heo  */
386137d3edbSTejun Heo void sg_miter_stop(struct sg_mapping_iter *miter)
387137d3edbSTejun Heo {
388137d3edbSTejun Heo 	WARN_ON(miter->consumed > miter->length);
389137d3edbSTejun Heo 
390137d3edbSTejun Heo 	/* drop resources from the last iteration */
391137d3edbSTejun Heo 	if (miter->addr) {
392137d3edbSTejun Heo 		miter->__offset += miter->consumed;
393137d3edbSTejun Heo 
394137d3edbSTejun Heo 		if (miter->__flags & SG_MITER_ATOMIC) {
395137d3edbSTejun Heo 			WARN_ON(!irqs_disabled());
396137d3edbSTejun Heo 			kunmap_atomic(miter->addr, KM_BIO_SRC_IRQ);
397137d3edbSTejun Heo 		} else
398137d3edbSTejun Heo 			kunmap(miter->addr);
399137d3edbSTejun Heo 
400137d3edbSTejun Heo 		miter->page = NULL;
401137d3edbSTejun Heo 		miter->addr = NULL;
402137d3edbSTejun Heo 		miter->length = 0;
403137d3edbSTejun Heo 		miter->consumed = 0;
404137d3edbSTejun Heo 	}
405137d3edbSTejun Heo }
406137d3edbSTejun Heo EXPORT_SYMBOL(sg_miter_stop);
407137d3edbSTejun Heo 
408137d3edbSTejun Heo /**
409b1adaf65SFUJITA Tomonori  * sg_copy_buffer - Copy data between a linear buffer and an SG list
410b1adaf65SFUJITA Tomonori  * @sgl:		 The SG list
411b1adaf65SFUJITA Tomonori  * @nents:		 Number of SG entries
412b1adaf65SFUJITA Tomonori  * @buf:		 Where to copy from
413b1adaf65SFUJITA Tomonori  * @buflen:		 The number of bytes to copy
414b1adaf65SFUJITA Tomonori  * @to_buffer: 		 transfer direction (non zero == from an sg list to a
415b1adaf65SFUJITA Tomonori  * 			 buffer, 0 == from a buffer to an sg list
416b1adaf65SFUJITA Tomonori  *
417b1adaf65SFUJITA Tomonori  * Returns the number of copied bytes.
418b1adaf65SFUJITA Tomonori  *
419b1adaf65SFUJITA Tomonori  **/
420b1adaf65SFUJITA Tomonori static size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents,
421b1adaf65SFUJITA Tomonori 			     void *buf, size_t buflen, int to_buffer)
422b1adaf65SFUJITA Tomonori {
423137d3edbSTejun Heo 	unsigned int offset = 0;
424137d3edbSTejun Heo 	struct sg_mapping_iter miter;
425b1adaf65SFUJITA Tomonori 
426137d3edbSTejun Heo 	sg_miter_start(&miter, sgl, nents, SG_MITER_ATOMIC);
427b1adaf65SFUJITA Tomonori 
428137d3edbSTejun Heo 	while (sg_miter_next(&miter) && offset < buflen) {
429137d3edbSTejun Heo 		unsigned int len;
430b1adaf65SFUJITA Tomonori 
431137d3edbSTejun Heo 		len = min(miter.length, buflen - offset);
432b1adaf65SFUJITA Tomonori 
433b1adaf65SFUJITA Tomonori 		if (to_buffer)
434137d3edbSTejun Heo 			memcpy(buf + offset, miter.addr, len);
435b1adaf65SFUJITA Tomonori 		else {
436137d3edbSTejun Heo 			memcpy(miter.addr, buf + offset, len);
437137d3edbSTejun Heo 			flush_kernel_dcache_page(miter.page);
438b1adaf65SFUJITA Tomonori 		}
439b1adaf65SFUJITA Tomonori 
440137d3edbSTejun Heo 		offset += len;
441b1adaf65SFUJITA Tomonori 	}
442b1adaf65SFUJITA Tomonori 
443137d3edbSTejun Heo 	sg_miter_stop(&miter);
444b1adaf65SFUJITA Tomonori 
445137d3edbSTejun Heo 	return offset;
446b1adaf65SFUJITA Tomonori }
447b1adaf65SFUJITA Tomonori 
448b1adaf65SFUJITA Tomonori /**
449b1adaf65SFUJITA Tomonori  * sg_copy_from_buffer - Copy from a linear buffer to an SG list
450b1adaf65SFUJITA Tomonori  * @sgl:		 The SG list
451b1adaf65SFUJITA Tomonori  * @nents:		 Number of SG entries
452b1adaf65SFUJITA Tomonori  * @buf:		 Where to copy from
453b1adaf65SFUJITA Tomonori  * @buflen:		 The number of bytes to copy
454b1adaf65SFUJITA Tomonori  *
455b1adaf65SFUJITA Tomonori  * Returns the number of copied bytes.
456b1adaf65SFUJITA Tomonori  *
457b1adaf65SFUJITA Tomonori  **/
458b1adaf65SFUJITA Tomonori size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
459b1adaf65SFUJITA Tomonori 			   void *buf, size_t buflen)
460b1adaf65SFUJITA Tomonori {
461b1adaf65SFUJITA Tomonori 	return sg_copy_buffer(sgl, nents, buf, buflen, 0);
462b1adaf65SFUJITA Tomonori }
463b1adaf65SFUJITA Tomonori EXPORT_SYMBOL(sg_copy_from_buffer);
464b1adaf65SFUJITA Tomonori 
465b1adaf65SFUJITA Tomonori /**
466b1adaf65SFUJITA Tomonori  * sg_copy_to_buffer - Copy from an SG list to a linear buffer
467b1adaf65SFUJITA Tomonori  * @sgl:		 The SG list
468b1adaf65SFUJITA Tomonori  * @nents:		 Number of SG entries
469b1adaf65SFUJITA Tomonori  * @buf:		 Where to copy to
470b1adaf65SFUJITA Tomonori  * @buflen:		 The number of bytes to copy
471b1adaf65SFUJITA Tomonori  *
472b1adaf65SFUJITA Tomonori  * Returns the number of copied bytes.
473b1adaf65SFUJITA Tomonori  *
474b1adaf65SFUJITA Tomonori  **/
475b1adaf65SFUJITA Tomonori size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
476b1adaf65SFUJITA Tomonori 			 void *buf, size_t buflen)
477b1adaf65SFUJITA Tomonori {
478b1adaf65SFUJITA Tomonori 	return sg_copy_buffer(sgl, nents, buf, buflen, 1);
479b1adaf65SFUJITA Tomonori }
480b1adaf65SFUJITA Tomonori EXPORT_SYMBOL(sg_copy_to_buffer);
481