18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci#include <linux/module.h>
38c2ecf20Sopenharmony_ci#include <linux/scatterlist.h>
48c2ecf20Sopenharmony_ci#include <linux/mempool.h>
58c2ecf20Sopenharmony_ci#include <linux/slab.h>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#define SG_MEMPOOL_NR		ARRAY_SIZE(sg_pools)
88c2ecf20Sopenharmony_ci#define SG_MEMPOOL_SIZE		2
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_cistruct sg_pool {
118c2ecf20Sopenharmony_ci	size_t		size;
128c2ecf20Sopenharmony_ci	char		*name;
138c2ecf20Sopenharmony_ci	struct kmem_cache	*slab;
148c2ecf20Sopenharmony_ci	mempool_t	*pool;
158c2ecf20Sopenharmony_ci};
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define SP(x) { .size = x, "sgpool-" __stringify(x) }
188c2ecf20Sopenharmony_ci#if (SG_CHUNK_SIZE < 32)
198c2ecf20Sopenharmony_ci#error SG_CHUNK_SIZE is too small (must be 32 or greater)
208c2ecf20Sopenharmony_ci#endif
218c2ecf20Sopenharmony_cistatic struct sg_pool sg_pools[] = {
228c2ecf20Sopenharmony_ci	SP(8),
238c2ecf20Sopenharmony_ci	SP(16),
248c2ecf20Sopenharmony_ci#if (SG_CHUNK_SIZE > 32)
258c2ecf20Sopenharmony_ci	SP(32),
268c2ecf20Sopenharmony_ci#if (SG_CHUNK_SIZE > 64)
278c2ecf20Sopenharmony_ci	SP(64),
288c2ecf20Sopenharmony_ci#if (SG_CHUNK_SIZE > 128)
298c2ecf20Sopenharmony_ci	SP(128),
308c2ecf20Sopenharmony_ci#if (SG_CHUNK_SIZE > 256)
318c2ecf20Sopenharmony_ci#error SG_CHUNK_SIZE is too large (256 MAX)
328c2ecf20Sopenharmony_ci#endif
338c2ecf20Sopenharmony_ci#endif
348c2ecf20Sopenharmony_ci#endif
358c2ecf20Sopenharmony_ci#endif
368c2ecf20Sopenharmony_ci	SP(SG_CHUNK_SIZE)
378c2ecf20Sopenharmony_ci};
388c2ecf20Sopenharmony_ci#undef SP
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic inline unsigned int sg_pool_index(unsigned short nents)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	unsigned int index;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	BUG_ON(nents > SG_CHUNK_SIZE);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	if (nents <= 8)
478c2ecf20Sopenharmony_ci		index = 0;
488c2ecf20Sopenharmony_ci	else
498c2ecf20Sopenharmony_ci		index = get_count_order(nents) - 3;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	return index;
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic void sg_pool_free(struct scatterlist *sgl, unsigned int nents)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	struct sg_pool *sgp;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	sgp = sg_pools + sg_pool_index(nents);
598c2ecf20Sopenharmony_ci	mempool_free(sgl, sgp->pool);
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic struct scatterlist *sg_pool_alloc(unsigned int nents, gfp_t gfp_mask)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	struct sg_pool *sgp;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	sgp = sg_pools + sg_pool_index(nents);
678c2ecf20Sopenharmony_ci	return mempool_alloc(sgp->pool, gfp_mask);
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci/**
718c2ecf20Sopenharmony_ci * sg_free_table_chained - Free a previously mapped sg table
728c2ecf20Sopenharmony_ci * @table:	The sg table header to use
738c2ecf20Sopenharmony_ci * @nents_first_chunk: size of the first_chunk SGL passed to
748c2ecf20Sopenharmony_ci *		sg_alloc_table_chained
758c2ecf20Sopenharmony_ci *
768c2ecf20Sopenharmony_ci *  Description:
778c2ecf20Sopenharmony_ci *    Free an sg table previously allocated and setup with
788c2ecf20Sopenharmony_ci *    sg_alloc_table_chained().
798c2ecf20Sopenharmony_ci *
808c2ecf20Sopenharmony_ci *    @nents_first_chunk has to be same with that same parameter passed
818c2ecf20Sopenharmony_ci *    to sg_alloc_table_chained().
828c2ecf20Sopenharmony_ci *
838c2ecf20Sopenharmony_ci **/
848c2ecf20Sopenharmony_civoid sg_free_table_chained(struct sg_table *table,
858c2ecf20Sopenharmony_ci		unsigned nents_first_chunk)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	if (table->orig_nents <= nents_first_chunk)
888c2ecf20Sopenharmony_ci		return;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	if (nents_first_chunk == 1)
918c2ecf20Sopenharmony_ci		nents_first_chunk = 0;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	__sg_free_table(table, SG_CHUNK_SIZE, nents_first_chunk, sg_pool_free);
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sg_free_table_chained);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci/**
988c2ecf20Sopenharmony_ci * sg_alloc_table_chained - Allocate and chain SGLs in an sg table
998c2ecf20Sopenharmony_ci * @table:	The sg table header to use
1008c2ecf20Sopenharmony_ci * @nents:	Number of entries in sg list
1018c2ecf20Sopenharmony_ci * @first_chunk: first SGL
1028c2ecf20Sopenharmony_ci * @nents_first_chunk: number of the SGL of @first_chunk
1038c2ecf20Sopenharmony_ci *
1048c2ecf20Sopenharmony_ci *  Description:
1058c2ecf20Sopenharmony_ci *    Allocate and chain SGLs in an sg table. If @nents@ is larger than
1068c2ecf20Sopenharmony_ci *    @nents_first_chunk a chained sg table will be setup. @first_chunk is
1078c2ecf20Sopenharmony_ci *    ignored if nents_first_chunk <= 1 because user expects the SGL points
1088c2ecf20Sopenharmony_ci *    non-chain SGL.
1098c2ecf20Sopenharmony_ci *
1108c2ecf20Sopenharmony_ci **/
1118c2ecf20Sopenharmony_ciint sg_alloc_table_chained(struct sg_table *table, int nents,
1128c2ecf20Sopenharmony_ci		struct scatterlist *first_chunk, unsigned nents_first_chunk)
1138c2ecf20Sopenharmony_ci{
1148c2ecf20Sopenharmony_ci	int ret;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	BUG_ON(!nents);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	if (first_chunk && nents_first_chunk) {
1198c2ecf20Sopenharmony_ci		if (nents <= nents_first_chunk) {
1208c2ecf20Sopenharmony_ci			table->nents = table->orig_nents = nents;
1218c2ecf20Sopenharmony_ci			sg_init_table(table->sgl, nents);
1228c2ecf20Sopenharmony_ci			return 0;
1238c2ecf20Sopenharmony_ci		}
1248c2ecf20Sopenharmony_ci	}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	/* User supposes that the 1st SGL includes real entry */
1278c2ecf20Sopenharmony_ci	if (nents_first_chunk <= 1) {
1288c2ecf20Sopenharmony_ci		first_chunk = NULL;
1298c2ecf20Sopenharmony_ci		nents_first_chunk = 0;
1308c2ecf20Sopenharmony_ci	}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	ret = __sg_alloc_table(table, nents, SG_CHUNK_SIZE,
1338c2ecf20Sopenharmony_ci			       first_chunk, nents_first_chunk,
1348c2ecf20Sopenharmony_ci			       GFP_ATOMIC, sg_pool_alloc);
1358c2ecf20Sopenharmony_ci	if (unlikely(ret))
1368c2ecf20Sopenharmony_ci		sg_free_table_chained(table, nents_first_chunk);
1378c2ecf20Sopenharmony_ci	return ret;
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sg_alloc_table_chained);
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cistatic __init int sg_pool_init(void)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	int i;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	for (i = 0; i < SG_MEMPOOL_NR; i++) {
1468c2ecf20Sopenharmony_ci		struct sg_pool *sgp = sg_pools + i;
1478c2ecf20Sopenharmony_ci		int size = sgp->size * sizeof(struct scatterlist);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci		sgp->slab = kmem_cache_create(sgp->name, size, 0,
1508c2ecf20Sopenharmony_ci				SLAB_HWCACHE_ALIGN, NULL);
1518c2ecf20Sopenharmony_ci		if (!sgp->slab) {
1528c2ecf20Sopenharmony_ci			printk(KERN_ERR "SG_POOL: can't init sg slab %s\n",
1538c2ecf20Sopenharmony_ci					sgp->name);
1548c2ecf20Sopenharmony_ci			goto cleanup_sdb;
1558c2ecf20Sopenharmony_ci		}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci		sgp->pool = mempool_create_slab_pool(SG_MEMPOOL_SIZE,
1588c2ecf20Sopenharmony_ci						     sgp->slab);
1598c2ecf20Sopenharmony_ci		if (!sgp->pool) {
1608c2ecf20Sopenharmony_ci			printk(KERN_ERR "SG_POOL: can't init sg mempool %s\n",
1618c2ecf20Sopenharmony_ci					sgp->name);
1628c2ecf20Sopenharmony_ci			goto cleanup_sdb;
1638c2ecf20Sopenharmony_ci		}
1648c2ecf20Sopenharmony_ci	}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	return 0;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cicleanup_sdb:
1698c2ecf20Sopenharmony_ci	for (i = 0; i < SG_MEMPOOL_NR; i++) {
1708c2ecf20Sopenharmony_ci		struct sg_pool *sgp = sg_pools + i;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci		mempool_destroy(sgp->pool);
1738c2ecf20Sopenharmony_ci		kmem_cache_destroy(sgp->slab);
1748c2ecf20Sopenharmony_ci	}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	return -ENOMEM;
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cistatic __exit void sg_pool_exit(void)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	int i;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	for (i = 0; i < SG_MEMPOOL_NR; i++) {
1848c2ecf20Sopenharmony_ci		struct sg_pool *sgp = sg_pools + i;
1858c2ecf20Sopenharmony_ci		mempool_destroy(sgp->pool);
1868c2ecf20Sopenharmony_ci		kmem_cache_destroy(sgp->slab);
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cimodule_init(sg_pool_init);
1918c2ecf20Sopenharmony_cimodule_exit(sg_pool_exit);
192