scatterlist.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright (C) 2007 Jens Axboe <jens.axboe@oracle.com>
  3. *
  4. * Scatterlist handling helpers.
  5. *
  6. * This source code is licensed under the GNU General Public License,
  7. * Version 2. See the file COPYING for more details.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/scatterlist.h>
  11. /**
  12. * sg_next - return the next scatterlist entry in a list
  13. * @sg: The current sg entry
  14. *
  15. * Description:
  16. * Usually the next entry will be @sg@ + 1, but if this sg element is part
  17. * of a chained scatterlist, it could jump to the start of a new
  18. * scatterlist array.
  19. *
  20. **/
  21. struct scatterlist *sg_next(struct scatterlist *sg)
  22. {
  23. #ifdef CONFIG_DEBUG_SG
  24. BUG_ON(sg->sg_magic != SG_MAGIC);
  25. #endif
  26. if (sg_is_last(sg))
  27. return NULL;
  28. sg++;
  29. if (unlikely(sg_is_chain(sg)))
  30. sg = sg_chain_ptr(sg);
  31. return sg;
  32. }
  33. EXPORT_SYMBOL(sg_next);
  34. /**
  35. * sg_last - return the last scatterlist entry in a list
  36. * @sgl: First entry in the scatterlist
  37. * @nents: Number of entries in the scatterlist
  38. *
  39. * Description:
  40. * Should only be used casually, it (currently) scans the entire list
  41. * to get the last entry.
  42. *
  43. * Note that the @sgl@ pointer passed in need not be the first one,
  44. * the important bit is that @nents@ denotes the number of entries that
  45. * exist from @sgl@.
  46. *
  47. **/
  48. struct scatterlist *sg_last(struct scatterlist *sgl, unsigned int nents)
  49. {
  50. #ifndef ARCH_HAS_SG_CHAIN
  51. struct scatterlist *ret = &sgl[nents - 1];
  52. #else
  53. struct scatterlist *sg, *ret = NULL;
  54. unsigned int i;
  55. for_each_sg(sgl, sg, nents, i)
  56. ret = sg;
  57. #endif
  58. #ifdef CONFIG_DEBUG_SG
  59. BUG_ON(sgl[0].sg_magic != SG_MAGIC);
  60. BUG_ON(!sg_is_last(ret));
  61. #endif
  62. return ret;
  63. }
  64. EXPORT_SYMBOL(sg_last);
  65. /**
  66. * sg_init_table - Initialize SG table
  67. * @sgl: The SG table
  68. * @nents: Number of entries in table
  69. *
  70. * Notes:
  71. * If this is part of a chained sg table, sg_mark_end() should be
  72. * used only on the last table part.
  73. *
  74. **/
  75. void sg_init_table(struct scatterlist *sgl, unsigned int nents)
  76. {
  77. memset(sgl, 0, sizeof(*sgl) * nents);
  78. #ifdef CONFIG_DEBUG_SG
  79. {
  80. unsigned int i;
  81. for (i = 0; i < nents; i++)
  82. sgl[i].sg_magic = SG_MAGIC;
  83. }
  84. #endif
  85. sg_mark_end(&sgl[nents - 1]);
  86. }
  87. EXPORT_SYMBOL(sg_init_table);
  88. /**
  89. * sg_init_one - Initialize a single entry sg list
  90. * @sg: SG entry
  91. * @buf: Virtual address for IO
  92. * @buflen: IO length
  93. *
  94. **/
  95. void sg_init_one(struct scatterlist *sg, const void *buf, unsigned int buflen)
  96. {
  97. sg_init_table(sg, 1);
  98. sg_set_buf(sg, buf, buflen);
  99. }
  100. EXPORT_SYMBOL(sg_init_one);
  101. /*
  102. * The default behaviour of sg_alloc_table() is to use these kmalloc/kfree
  103. * helpers.
  104. */
  105. static struct scatterlist *sg_kmalloc(unsigned int nents, gfp_t gfp_mask)
  106. {
  107. if (nents == SG_MAX_SINGLE_ALLOC)
  108. return (struct scatterlist *) __get_free_page(gfp_mask);
  109. else
  110. return kmalloc(nents * sizeof(struct scatterlist), gfp_mask);
  111. }
  112. static void sg_kfree(struct scatterlist *sg, unsigned int nents)
  113. {
  114. if (nents == SG_MAX_SINGLE_ALLOC)
  115. free_page((unsigned long) sg);
  116. else
  117. kfree(sg);
  118. }
  119. /**
  120. * __sg_free_table - Free a previously mapped sg table
  121. * @table: The sg table header to use
  122. * @max_ents: The maximum number of entries per single scatterlist
  123. * @free_fn: Free function
  124. *
  125. * Description:
  126. * Free an sg table previously allocated and setup with
  127. * __sg_alloc_table(). The @max_ents value must be identical to
  128. * that previously used with __sg_alloc_table().
  129. *
  130. **/
  131. void __sg_free_table(struct sg_table *table, unsigned int max_ents,
  132. sg_free_fn *free_fn)
  133. {
  134. struct scatterlist *sgl, *next;
  135. if (unlikely(!table->sgl))
  136. return;
  137. sgl = table->sgl;
  138. while (table->orig_nents) {
  139. unsigned int alloc_size = table->orig_nents;
  140. unsigned int sg_size;
  141. /*
  142. * If we have more than max_ents segments left,
  143. * then assign 'next' to the sg table after the current one.
  144. * sg_size is then one less than alloc size, since the last
  145. * element is the chain pointer.
  146. */
  147. if (alloc_size > max_ents) {
  148. next = sg_chain_ptr(&sgl[max_ents - 1]);
  149. alloc_size = max_ents;
  150. sg_size = alloc_size - 1;
  151. } else {
  152. sg_size = alloc_size;
  153. next = NULL;
  154. }
  155. table->orig_nents -= sg_size;
  156. free_fn(sgl, alloc_size);
  157. sgl = next;
  158. }
  159. table->sgl = NULL;
  160. }
  161. EXPORT_SYMBOL(__sg_free_table);
  162. /**
  163. * sg_free_table - Free a previously allocated sg table
  164. * @table: The mapped sg table header
  165. *
  166. **/
  167. void sg_free_table(struct sg_table *table)
  168. {
  169. __sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree);
  170. }
  171. EXPORT_SYMBOL(sg_free_table);
  172. /**
  173. * __sg_alloc_table - Allocate and initialize an sg table with given allocator
  174. * @table: The sg table header to use
  175. * @nents: Number of entries in sg list
  176. * @max_ents: The maximum number of entries the allocator returns per call
  177. * @gfp_mask: GFP allocation mask
  178. * @alloc_fn: Allocator to use
  179. *
  180. * Description:
  181. * This function returns a @table @nents long. The allocator is
  182. * defined to return scatterlist chunks of maximum size @max_ents.
  183. * Thus if @nents is bigger than @max_ents, the scatterlists will be
  184. * chained in units of @max_ents.
  185. *
  186. * Notes:
  187. * If this function returns non-0 (eg failure), the caller must call
  188. * __sg_free_table() to cleanup any leftover allocations.
  189. *
  190. **/
  191. int __sg_alloc_table(struct sg_table *table, unsigned int nents,
  192. unsigned int max_ents, gfp_t gfp_mask,
  193. sg_alloc_fn *alloc_fn)
  194. {
  195. struct scatterlist *sg, *prv;
  196. unsigned int left;
  197. #ifndef ARCH_HAS_SG_CHAIN
  198. BUG_ON(nents > max_ents);
  199. #endif
  200. memset(table, 0, sizeof(*table));
  201. left = nents;
  202. prv = NULL;
  203. do {
  204. unsigned int sg_size, alloc_size = left;
  205. if (alloc_size > max_ents) {
  206. alloc_size = max_ents;
  207. sg_size = alloc_size - 1;
  208. } else
  209. sg_size = alloc_size;
  210. left -= sg_size;
  211. sg = alloc_fn(alloc_size, gfp_mask);
  212. if (unlikely(!sg))
  213. return -ENOMEM;
  214. sg_init_table(sg, alloc_size);
  215. table->nents = table->orig_nents += sg_size;
  216. /*
  217. * If this is the first mapping, assign the sg table header.
  218. * If this is not the first mapping, chain previous part.
  219. */
  220. if (prv)
  221. sg_chain(prv, max_ents, sg);
  222. else
  223. table->sgl = sg;
  224. /*
  225. * If no more entries after this one, mark the end
  226. */
  227. if (!left)
  228. sg_mark_end(&sg[sg_size - 1]);
  229. /*
  230. * only really needed for mempool backed sg allocations (like
  231. * SCSI), a possible improvement here would be to pass the
  232. * table pointer into the allocator and let that clear these
  233. * flags
  234. */
  235. gfp_mask &= ~__GFP_WAIT;
  236. gfp_mask |= __GFP_HIGH;
  237. prv = sg;
  238. } while (left);
  239. return 0;
  240. }
  241. EXPORT_SYMBOL(__sg_alloc_table);
  242. /**
  243. * sg_alloc_table - Allocate and initialize an sg table
  244. * @table: The sg table header to use
  245. * @nents: Number of entries in sg list
  246. * @gfp_mask: GFP allocation mask
  247. *
  248. * Description:
  249. * Allocate and initialize an sg table. If @nents@ is larger than
  250. * SG_MAX_SINGLE_ALLOC a chained sg table will be setup.
  251. *
  252. **/
  253. int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
  254. {
  255. int ret;
  256. ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
  257. gfp_mask, sg_kmalloc);
  258. if (unlikely(ret))
  259. __sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree);
  260. return ret;
  261. }
  262. EXPORT_SYMBOL(sg_alloc_table);