scatterlist.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. #include <linux/highmem.h>
  12. /**
  13. * sg_next - return the next scatterlist entry in a list
  14. * @sg: The current sg entry
  15. *
  16. * Description:
  17. * Usually the next entry will be @sg@ + 1, but if this sg element is part
  18. * of a chained scatterlist, it could jump to the start of a new
  19. * scatterlist array.
  20. *
  21. **/
  22. struct scatterlist *sg_next(struct scatterlist *sg)
  23. {
  24. #ifdef CONFIG_DEBUG_SG
  25. BUG_ON(sg->sg_magic != SG_MAGIC);
  26. #endif
  27. if (sg_is_last(sg))
  28. return NULL;
  29. sg++;
  30. if (unlikely(sg_is_chain(sg)))
  31. sg = sg_chain_ptr(sg);
  32. return sg;
  33. }
  34. EXPORT_SYMBOL(sg_next);
  35. /**
  36. * sg_last - return the last scatterlist entry in a list
  37. * @sgl: First entry in the scatterlist
  38. * @nents: Number of entries in the scatterlist
  39. *
  40. * Description:
  41. * Should only be used casually, it (currently) scans the entire list
  42. * to get the last entry.
  43. *
  44. * Note that the @sgl@ pointer passed in need not be the first one,
  45. * the important bit is that @nents@ denotes the number of entries that
  46. * exist from @sgl@.
  47. *
  48. **/
  49. struct scatterlist *sg_last(struct scatterlist *sgl, unsigned int nents)
  50. {
  51. #ifndef ARCH_HAS_SG_CHAIN
  52. struct scatterlist *ret = &sgl[nents - 1];
  53. #else
  54. struct scatterlist *sg, *ret = NULL;
  55. unsigned int i;
  56. for_each_sg(sgl, sg, nents, i)
  57. ret = sg;
  58. #endif
  59. #ifdef CONFIG_DEBUG_SG
  60. BUG_ON(sgl[0].sg_magic != SG_MAGIC);
  61. BUG_ON(!sg_is_last(ret));
  62. #endif
  63. return ret;
  64. }
  65. EXPORT_SYMBOL(sg_last);
  66. /**
  67. * sg_init_table - Initialize SG table
  68. * @sgl: The SG table
  69. * @nents: Number of entries in table
  70. *
  71. * Notes:
  72. * If this is part of a chained sg table, sg_mark_end() should be
  73. * used only on the last table part.
  74. *
  75. **/
  76. void sg_init_table(struct scatterlist *sgl, unsigned int nents)
  77. {
  78. memset(sgl, 0, sizeof(*sgl) * nents);
  79. #ifdef CONFIG_DEBUG_SG
  80. {
  81. unsigned int i;
  82. for (i = 0; i < nents; i++)
  83. sgl[i].sg_magic = SG_MAGIC;
  84. }
  85. #endif
  86. sg_mark_end(&sgl[nents - 1]);
  87. }
  88. EXPORT_SYMBOL(sg_init_table);
  89. /**
  90. * sg_init_one - Initialize a single entry sg list
  91. * @sg: SG entry
  92. * @buf: Virtual address for IO
  93. * @buflen: IO length
  94. *
  95. **/
  96. void sg_init_one(struct scatterlist *sg, const void *buf, unsigned int buflen)
  97. {
  98. sg_init_table(sg, 1);
  99. sg_set_buf(sg, buf, buflen);
  100. }
  101. EXPORT_SYMBOL(sg_init_one);
  102. /*
  103. * The default behaviour of sg_alloc_table() is to use these kmalloc/kfree
  104. * helpers.
  105. */
  106. static struct scatterlist *sg_kmalloc(unsigned int nents, gfp_t gfp_mask)
  107. {
  108. if (nents == SG_MAX_SINGLE_ALLOC)
  109. return (struct scatterlist *) __get_free_page(gfp_mask);
  110. else
  111. return kmalloc(nents * sizeof(struct scatterlist), gfp_mask);
  112. }
  113. static void sg_kfree(struct scatterlist *sg, unsigned int nents)
  114. {
  115. if (nents == SG_MAX_SINGLE_ALLOC)
  116. free_page((unsigned long) sg);
  117. else
  118. kfree(sg);
  119. }
  120. /**
  121. * __sg_free_table - Free a previously mapped sg table
  122. * @table: The sg table header to use
  123. * @max_ents: The maximum number of entries per single scatterlist
  124. * @free_fn: Free function
  125. *
  126. * Description:
  127. * Free an sg table previously allocated and setup with
  128. * __sg_alloc_table(). The @max_ents value must be identical to
  129. * that previously used with __sg_alloc_table().
  130. *
  131. **/
  132. void __sg_free_table(struct sg_table *table, unsigned int max_ents,
  133. sg_free_fn *free_fn)
  134. {
  135. struct scatterlist *sgl, *next;
  136. if (unlikely(!table->sgl))
  137. return;
  138. sgl = table->sgl;
  139. while (table->orig_nents) {
  140. unsigned int alloc_size = table->orig_nents;
  141. unsigned int sg_size;
  142. /*
  143. * If we have more than max_ents segments left,
  144. * then assign 'next' to the sg table after the current one.
  145. * sg_size is then one less than alloc size, since the last
  146. * element is the chain pointer.
  147. */
  148. if (alloc_size > max_ents) {
  149. next = sg_chain_ptr(&sgl[max_ents - 1]);
  150. alloc_size = max_ents;
  151. sg_size = alloc_size - 1;
  152. } else {
  153. sg_size = alloc_size;
  154. next = NULL;
  155. }
  156. table->orig_nents -= sg_size;
  157. free_fn(sgl, alloc_size);
  158. sgl = next;
  159. }
  160. table->sgl = NULL;
  161. }
  162. EXPORT_SYMBOL(__sg_free_table);
  163. /**
  164. * sg_free_table - Free a previously allocated sg table
  165. * @table: The mapped sg table header
  166. *
  167. **/
  168. void sg_free_table(struct sg_table *table)
  169. {
  170. __sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree);
  171. }
  172. EXPORT_SYMBOL(sg_free_table);
  173. /**
  174. * __sg_alloc_table - Allocate and initialize an sg table with given allocator
  175. * @table: The sg table header to use
  176. * @nents: Number of entries in sg list
  177. * @max_ents: The maximum number of entries the allocator returns per call
  178. * @gfp_mask: GFP allocation mask
  179. * @alloc_fn: Allocator to use
  180. *
  181. * Description:
  182. * This function returns a @table @nents long. The allocator is
  183. * defined to return scatterlist chunks of maximum size @max_ents.
  184. * Thus if @nents is bigger than @max_ents, the scatterlists will be
  185. * chained in units of @max_ents.
  186. *
  187. * Notes:
  188. * If this function returns non-0 (eg failure), the caller must call
  189. * __sg_free_table() to cleanup any leftover allocations.
  190. *
  191. **/
  192. int __sg_alloc_table(struct sg_table *table, unsigned int nents,
  193. unsigned int max_ents, gfp_t gfp_mask,
  194. sg_alloc_fn *alloc_fn)
  195. {
  196. struct scatterlist *sg, *prv;
  197. unsigned int left;
  198. #ifndef ARCH_HAS_SG_CHAIN
  199. BUG_ON(nents > max_ents);
  200. #endif
  201. memset(table, 0, sizeof(*table));
  202. left = nents;
  203. prv = NULL;
  204. do {
  205. unsigned int sg_size, alloc_size = left;
  206. if (alloc_size > max_ents) {
  207. alloc_size = max_ents;
  208. sg_size = alloc_size - 1;
  209. } else
  210. sg_size = alloc_size;
  211. left -= sg_size;
  212. sg = alloc_fn(alloc_size, gfp_mask);
  213. if (unlikely(!sg))
  214. return -ENOMEM;
  215. sg_init_table(sg, alloc_size);
  216. table->nents = table->orig_nents += sg_size;
  217. /*
  218. * If this is the first mapping, assign the sg table header.
  219. * If this is not the first mapping, chain previous part.
  220. */
  221. if (prv)
  222. sg_chain(prv, max_ents, sg);
  223. else
  224. table->sgl = sg;
  225. /*
  226. * If no more entries after this one, mark the end
  227. */
  228. if (!left)
  229. sg_mark_end(&sg[sg_size - 1]);
  230. /*
  231. * only really needed for mempool backed sg allocations (like
  232. * SCSI), a possible improvement here would be to pass the
  233. * table pointer into the allocator and let that clear these
  234. * flags
  235. */
  236. gfp_mask &= ~__GFP_WAIT;
  237. gfp_mask |= __GFP_HIGH;
  238. prv = sg;
  239. } while (left);
  240. return 0;
  241. }
  242. EXPORT_SYMBOL(__sg_alloc_table);
  243. /**
  244. * sg_alloc_table - Allocate and initialize an sg table
  245. * @table: The sg table header to use
  246. * @nents: Number of entries in sg list
  247. * @gfp_mask: GFP allocation mask
  248. *
  249. * Description:
  250. * Allocate and initialize an sg table. If @nents@ is larger than
  251. * SG_MAX_SINGLE_ALLOC a chained sg table will be setup.
  252. *
  253. **/
  254. int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
  255. {
  256. int ret;
  257. ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
  258. gfp_mask, sg_kmalloc);
  259. if (unlikely(ret))
  260. __sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree);
  261. return ret;
  262. }
  263. EXPORT_SYMBOL(sg_alloc_table);
  264. /**
  265. * sg_copy_buffer - Copy data between a linear buffer and an SG list
  266. * @sgl: The SG list
  267. * @nents: Number of SG entries
  268. * @buf: Where to copy from
  269. * @buflen: The number of bytes to copy
  270. * @to_buffer: transfer direction (non zero == from an sg list to a
  271. * buffer, 0 == from a buffer to an sg list
  272. *
  273. * Returns the number of copied bytes.
  274. *
  275. **/
  276. static size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents,
  277. void *buf, size_t buflen, int to_buffer)
  278. {
  279. struct scatterlist *sg;
  280. size_t buf_off = 0;
  281. int i;
  282. WARN_ON(!irqs_disabled());
  283. for_each_sg(sgl, sg, nents, i) {
  284. struct page *page;
  285. int n = 0;
  286. unsigned int sg_off = sg->offset;
  287. unsigned int sg_copy = sg->length;
  288. if (sg_copy > buflen)
  289. sg_copy = buflen;
  290. buflen -= sg_copy;
  291. while (sg_copy > 0) {
  292. unsigned int page_copy;
  293. void *p;
  294. page_copy = PAGE_SIZE - sg_off;
  295. if (page_copy > sg_copy)
  296. page_copy = sg_copy;
  297. page = nth_page(sg_page(sg), n);
  298. p = kmap_atomic(page, KM_BIO_SRC_IRQ);
  299. if (to_buffer)
  300. memcpy(buf + buf_off, p + sg_off, page_copy);
  301. else {
  302. memcpy(p + sg_off, buf + buf_off, page_copy);
  303. flush_kernel_dcache_page(page);
  304. }
  305. kunmap_atomic(p, KM_BIO_SRC_IRQ);
  306. buf_off += page_copy;
  307. sg_off += page_copy;
  308. if (sg_off == PAGE_SIZE) {
  309. sg_off = 0;
  310. n++;
  311. }
  312. sg_copy -= page_copy;
  313. }
  314. if (!buflen)
  315. break;
  316. }
  317. return buf_off;
  318. }
  319. /**
  320. * sg_copy_from_buffer - Copy from a linear buffer to an SG list
  321. * @sgl: The SG list
  322. * @nents: Number of SG entries
  323. * @buf: Where to copy from
  324. * @buflen: The number of bytes to copy
  325. *
  326. * Returns the number of copied bytes.
  327. *
  328. **/
  329. size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
  330. void *buf, size_t buflen)
  331. {
  332. return sg_copy_buffer(sgl, nents, buf, buflen, 0);
  333. }
  334. EXPORT_SYMBOL(sg_copy_from_buffer);
  335. /**
  336. * sg_copy_to_buffer - Copy from an SG list to a linear buffer
  337. * @sgl: The SG list
  338. * @nents: Number of SG entries
  339. * @buf: Where to copy to
  340. * @buflen: The number of bytes to copy
  341. *
  342. * Returns the number of copied bytes.
  343. *
  344. **/
  345. size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
  346. void *buf, size_t buflen)
  347. {
  348. return sg_copy_buffer(sgl, nents, buf, buflen, 1);
  349. }
  350. EXPORT_SYMBOL(sg_copy_to_buffer);