scatterlist.h 10 KB

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