scatterlist.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #ifndef _LINUX_SCATTERLIST_H
  2. #define _LINUX_SCATTERLIST_H
  3. #include <linux/string.h>
  4. #include <linux/types.h>
  5. #include <linux/bug.h>
  6. #include <linux/mm.h>
  7. #include <asm/io.h>
  8. struct scatterlist {
  9. #ifdef CONFIG_DEBUG_SG
  10. unsigned long sg_magic;
  11. #endif
  12. unsigned long page_link;
  13. unsigned int offset;
  14. unsigned int length;
  15. dma_addr_t dma_address;
  16. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  17. unsigned int dma_length;
  18. #endif
  19. };
  20. /*
  21. * These macros should be used after a dma_map_sg call has been done
  22. * to get bus addresses of each of the SG entries and their lengths.
  23. * You should only work with the number of sg entries dma_map_sg
  24. * returns, or alternatively stop on the first sg_dma_len(sg) which
  25. * is 0.
  26. */
  27. #define sg_dma_address(sg) ((sg)->dma_address)
  28. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  29. #define sg_dma_len(sg) ((sg)->dma_length)
  30. #else
  31. #define sg_dma_len(sg) ((sg)->length)
  32. #endif
  33. struct sg_table {
  34. struct scatterlist *sgl; /* the list */
  35. unsigned int nents; /* number of mapped entries */
  36. unsigned int orig_nents; /* original size of list */
  37. };
  38. /*
  39. * Notes on SG table design.
  40. *
  41. * We use the unsigned long page_link field in the scatterlist struct to place
  42. * the page pointer AND encode information about the sg table as well. The two
  43. * lower bits are reserved for this information.
  44. *
  45. * If bit 0 is set, then the page_link contains a pointer to the next sg
  46. * table list. Otherwise the next entry is at sg + 1.
  47. *
  48. * If bit 1 is set, then this sg entry is the last element in a list.
  49. *
  50. * See sg_next().
  51. *
  52. */
  53. #define SG_MAGIC 0x87654321
  54. /*
  55. * We overload the LSB of the page pointer to indicate whether it's
  56. * a valid sg entry, or whether it points to the start of a new scatterlist.
  57. * Those low bits are there for everyone! (thanks mason :-)
  58. */
  59. #define sg_is_chain(sg) ((sg)->page_link & 0x01)
  60. #define sg_is_last(sg) ((sg)->page_link & 0x02)
  61. #define sg_chain_ptr(sg) \
  62. ((struct scatterlist *) ((sg)->page_link & ~0x03))
  63. /**
  64. * sg_assign_page - Assign a given page to an SG entry
  65. * @sg: SG entry
  66. * @page: The page
  67. *
  68. * Description:
  69. * Assign page to sg entry. Also see sg_set_page(), the most commonly used
  70. * variant.
  71. *
  72. **/
  73. static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
  74. {
  75. unsigned long page_link = sg->page_link & 0x3;
  76. /*
  77. * In order for the low bit stealing approach to work, pages
  78. * must be aligned at a 32-bit boundary as a minimum.
  79. */
  80. BUG_ON((unsigned long) page & 0x03);
  81. #ifdef CONFIG_DEBUG_SG
  82. BUG_ON(sg->sg_magic != SG_MAGIC);
  83. BUG_ON(sg_is_chain(sg));
  84. #endif
  85. sg->page_link = page_link | (unsigned long) page;
  86. }
  87. /**
  88. * sg_set_page - Set sg entry to point at given page
  89. * @sg: SG entry
  90. * @page: The page
  91. * @len: Length of data
  92. * @offset: Offset into page
  93. *
  94. * Description:
  95. * Use this function to set an sg entry pointing at a page, never assign
  96. * the page directly. We encode sg table information in the lower bits
  97. * of the page pointer. See sg_page() for looking up the page belonging
  98. * to an sg entry.
  99. *
  100. **/
  101. static inline void sg_set_page(struct scatterlist *sg, struct page *page,
  102. unsigned int len, unsigned int offset)
  103. {
  104. sg_assign_page(sg, page);
  105. sg->offset = offset;
  106. sg->length = len;
  107. }
  108. static inline struct page *sg_page(struct scatterlist *sg)
  109. {
  110. #ifdef CONFIG_DEBUG_SG
  111. BUG_ON(sg->sg_magic != SG_MAGIC);
  112. BUG_ON(sg_is_chain(sg));
  113. #endif
  114. return (struct page *)((sg)->page_link & ~0x3);
  115. }
  116. /**
  117. * sg_set_buf - Set sg entry to point at given data
  118. * @sg: SG entry
  119. * @buf: Data
  120. * @buflen: Data length
  121. *
  122. **/
  123. static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
  124. unsigned int buflen)
  125. {
  126. #ifdef CONFIG_DEBUG_SG
  127. BUG_ON(!virt_addr_valid(buf));
  128. #endif
  129. sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
  130. }
  131. /*
  132. * Loop over each sg element, following the pointer to a new list if necessary
  133. */
  134. #define for_each_sg(sglist, sg, nr, __i) \
  135. for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
  136. /**
  137. * sg_chain - Chain two sglists together
  138. * @prv: First scatterlist
  139. * @prv_nents: Number of entries in prv
  140. * @sgl: Second scatterlist
  141. *
  142. * Description:
  143. * Links @prv@ and @sgl@ together, to form a longer scatterlist.
  144. *
  145. **/
  146. static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents,
  147. struct scatterlist *sgl)
  148. {
  149. /*
  150. * offset and length are unused for chain entry. Clear them.
  151. */
  152. prv[prv_nents - 1].offset = 0;
  153. prv[prv_nents - 1].length = 0;
  154. /*
  155. * Set lowest bit to indicate a link pointer, and make sure to clear
  156. * the termination bit if it happens to be set.
  157. */
  158. prv[prv_nents - 1].page_link = ((unsigned long) sgl | 0x01) & ~0x02;
  159. }
  160. /**
  161. * sg_mark_end - Mark the end of the scatterlist
  162. * @sg: SG entryScatterlist
  163. *
  164. * Description:
  165. * Marks the passed in sg entry as the termination point for the sg
  166. * table. A call to sg_next() on this entry will return NULL.
  167. *
  168. **/
  169. static inline void sg_mark_end(struct scatterlist *sg)
  170. {
  171. #ifdef CONFIG_DEBUG_SG
  172. BUG_ON(sg->sg_magic != SG_MAGIC);
  173. #endif
  174. /*
  175. * Set termination bit, clear potential chain bit
  176. */
  177. sg->page_link |= 0x02;
  178. sg->page_link &= ~0x01;
  179. }
  180. /**
  181. * sg_unmark_end - Undo setting the end of the scatterlist
  182. * @sg: SG entryScatterlist
  183. *
  184. * Description:
  185. * Removes the termination marker from the given entry of the scatterlist.
  186. *
  187. **/
  188. static inline void sg_unmark_end(struct scatterlist *sg)
  189. {
  190. #ifdef CONFIG_DEBUG_SG
  191. BUG_ON(sg->sg_magic != SG_MAGIC);
  192. #endif
  193. sg->page_link &= ~0x02;
  194. }
  195. /**
  196. * sg_phys - Return physical address of an sg entry
  197. * @sg: SG entry
  198. *
  199. * Description:
  200. * This calls page_to_phys() on the page in this sg entry, and adds the
  201. * sg offset. The caller must know that it is legal to call page_to_phys()
  202. * on the sg page.
  203. *
  204. **/
  205. static inline dma_addr_t sg_phys(struct scatterlist *sg)
  206. {
  207. return page_to_phys(sg_page(sg)) + sg->offset;
  208. }
  209. /**
  210. * sg_virt - Return virtual address of an sg entry
  211. * @sg: SG entry
  212. *
  213. * Description:
  214. * This calls page_address() on the page in this sg entry, and adds the
  215. * sg offset. The caller must know that the sg page has a valid virtual
  216. * mapping.
  217. *
  218. **/
  219. static inline void *sg_virt(struct scatterlist *sg)
  220. {
  221. return page_address(sg_page(sg)) + sg->offset;
  222. }
  223. int sg_nents(struct scatterlist *sg);
  224. int sg_nents_for_len(struct scatterlist *sg, u64 len);
  225. struct scatterlist *sg_next(struct scatterlist *);
  226. struct scatterlist *sg_last(struct scatterlist *s, unsigned int);
  227. void sg_init_table(struct scatterlist *, unsigned int);
  228. void sg_init_one(struct scatterlist *, const void *, unsigned int);
  229. int sg_split(struct scatterlist *in, const int in_mapped_nents,
  230. const off_t skip, const int nb_splits,
  231. const size_t *split_sizes,
  232. struct scatterlist **out, int *out_mapped_nents,
  233. gfp_t gfp_mask);
  234. typedef struct scatterlist *(sg_alloc_fn)(unsigned int, gfp_t);
  235. typedef void (sg_free_fn)(struct scatterlist *, unsigned int);
  236. void __sg_free_table(struct sg_table *, unsigned int, bool, sg_free_fn *);
  237. void sg_free_table(struct sg_table *);
  238. int __sg_alloc_table(struct sg_table *, unsigned int, unsigned int,
  239. struct scatterlist *, gfp_t, sg_alloc_fn *);
  240. int sg_alloc_table(struct sg_table *, unsigned int, gfp_t);
  241. int sg_alloc_table_from_pages(struct sg_table *sgt,
  242. struct page **pages, unsigned int n_pages,
  243. unsigned long offset, unsigned long size,
  244. gfp_t gfp_mask);
  245. size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf,
  246. size_t buflen, off_t skip, bool to_buffer);
  247. size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
  248. const void *buf, size_t buflen);
  249. size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
  250. void *buf, size_t buflen);
  251. size_t sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents,
  252. const void *buf, size_t buflen, off_t skip);
  253. size_t sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents,
  254. void *buf, size_t buflen, off_t skip);
  255. /*
  256. * Maximum number of entries that will be allocated in one piece, if
  257. * a list larger than this is required then chaining will be utilized.
  258. */
  259. #define SG_MAX_SINGLE_ALLOC (PAGE_SIZE / sizeof(struct scatterlist))
  260. /*
  261. * The maximum number of SG segments that we will put inside a
  262. * scatterlist (unless chaining is used). Should ideally fit inside a
  263. * single page, to avoid a higher order allocation. We could define this
  264. * to SG_MAX_SINGLE_ALLOC to pack correctly at the highest order. The
  265. * minimum value is 32
  266. */
  267. #define SG_CHUNK_SIZE 128
  268. /*
  269. * Like SG_CHUNK_SIZE, but for archs that have sg chaining. This limit
  270. * is totally arbitrary, a setting of 2048 will get you at least 8mb ios.
  271. */
  272. #ifdef CONFIG_ARCH_HAS_SG_CHAIN
  273. #define SG_MAX_SEGMENTS 2048
  274. #else
  275. #define SG_MAX_SEGMENTS SG_CHUNK_SIZE
  276. #endif
  277. #ifdef CONFIG_SG_POOL
  278. void sg_free_table_chained(struct sg_table *table, bool first_chunk);
  279. int sg_alloc_table_chained(struct sg_table *table, int nents,
  280. struct scatterlist *first_chunk);
  281. #endif
  282. /*
  283. * sg page iterator
  284. *
  285. * Iterates over sg entries page-by-page. On each successful iteration,
  286. * you can call sg_page_iter_page(@piter) and sg_page_iter_dma_address(@piter)
  287. * to get the current page and its dma address. @piter->sg will point to the
  288. * sg holding this page and @piter->sg_pgoffset to the page's page offset
  289. * within the sg. The iteration will stop either when a maximum number of sg
  290. * entries was reached or a terminating sg (sg_last(sg) == true) was reached.
  291. */
  292. struct sg_page_iter {
  293. struct scatterlist *sg; /* sg holding the page */
  294. unsigned int sg_pgoffset; /* page offset within the sg */
  295. /* these are internal states, keep away */
  296. unsigned int __nents; /* remaining sg entries */
  297. int __pg_advance; /* nr pages to advance at the
  298. * next step */
  299. };
  300. bool __sg_page_iter_next(struct sg_page_iter *piter);
  301. void __sg_page_iter_start(struct sg_page_iter *piter,
  302. struct scatterlist *sglist, unsigned int nents,
  303. unsigned long pgoffset);
  304. /**
  305. * sg_page_iter_page - get the current page held by the page iterator
  306. * @piter: page iterator holding the page
  307. */
  308. static inline struct page *sg_page_iter_page(struct sg_page_iter *piter)
  309. {
  310. return nth_page(sg_page(piter->sg), piter->sg_pgoffset);
  311. }
  312. /**
  313. * sg_page_iter_dma_address - get the dma address of the current page held by
  314. * the page iterator.
  315. * @piter: page iterator holding the page
  316. */
  317. static inline dma_addr_t sg_page_iter_dma_address(struct sg_page_iter *piter)
  318. {
  319. return sg_dma_address(piter->sg) + (piter->sg_pgoffset << PAGE_SHIFT);
  320. }
  321. /**
  322. * for_each_sg_page - iterate over the pages of the given sg list
  323. * @sglist: sglist to iterate over
  324. * @piter: page iterator to hold current page, sg, sg_pgoffset
  325. * @nents: maximum number of sg entries to iterate over
  326. * @pgoffset: starting page offset
  327. */
  328. #define for_each_sg_page(sglist, piter, nents, pgoffset) \
  329. for (__sg_page_iter_start((piter), (sglist), (nents), (pgoffset)); \
  330. __sg_page_iter_next(piter);)
  331. /*
  332. * Mapping sg iterator
  333. *
  334. * Iterates over sg entries mapping page-by-page. On each successful
  335. * iteration, @miter->page points to the mapped page and
  336. * @miter->length bytes of data can be accessed at @miter->addr. As
  337. * long as an interation is enclosed between start and stop, the user
  338. * is free to choose control structure and when to stop.
  339. *
  340. * @miter->consumed is set to @miter->length on each iteration. It
  341. * can be adjusted if the user can't consume all the bytes in one go.
  342. * Also, a stopped iteration can be resumed by calling next on it.
  343. * This is useful when iteration needs to release all resources and
  344. * continue later (e.g. at the next interrupt).
  345. */
  346. #define SG_MITER_ATOMIC (1 << 0) /* use kmap_atomic */
  347. #define SG_MITER_TO_SG (1 << 1) /* flush back to phys on unmap */
  348. #define SG_MITER_FROM_SG (1 << 2) /* nop */
  349. struct sg_mapping_iter {
  350. /* the following three fields can be accessed directly */
  351. struct page *page; /* currently mapped page */
  352. void *addr; /* pointer to the mapped area */
  353. size_t length; /* length of the mapped area */
  354. size_t consumed; /* number of consumed bytes */
  355. struct sg_page_iter piter; /* page iterator */
  356. /* these are internal states, keep away */
  357. unsigned int __offset; /* offset within page */
  358. unsigned int __remaining; /* remaining bytes on page */
  359. unsigned int __flags;
  360. };
  361. void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
  362. unsigned int nents, unsigned int flags);
  363. bool sg_miter_skip(struct sg_mapping_iter *miter, off_t offset);
  364. bool sg_miter_next(struct sg_mapping_iter *miter);
  365. void sg_miter_stop(struct sg_mapping_iter *miter);
  366. #endif /* _LINUX_SCATTERLIST_H */