scatterlist.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
  104. }
  105. /*
  106. * Loop over each sg element, following the pointer to a new list if necessary
  107. */
  108. #define for_each_sg(sglist, sg, nr, __i) \
  109. for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
  110. /**
  111. * sg_chain - Chain two sglists together
  112. * @prv: First scatterlist
  113. * @prv_nents: Number of entries in prv
  114. * @sgl: Second scatterlist
  115. *
  116. * Description:
  117. * Links @prv@ and @sgl@ together, to form a longer scatterlist.
  118. *
  119. **/
  120. static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents,
  121. struct scatterlist *sgl)
  122. {
  123. #ifndef ARCH_HAS_SG_CHAIN
  124. BUG();
  125. #endif
  126. /*
  127. * offset and length are unused for chain entry. Clear them.
  128. */
  129. prv[prv_nents - 1].offset = 0;
  130. prv[prv_nents - 1].length = 0;
  131. /*
  132. * Set lowest bit to indicate a link pointer, and make sure to clear
  133. * the termination bit if it happens to be set.
  134. */
  135. prv[prv_nents - 1].page_link = ((unsigned long) sgl | 0x01) & ~0x02;
  136. }
  137. /**
  138. * sg_mark_end - Mark the end of the scatterlist
  139. * @sg: SG entryScatterlist
  140. *
  141. * Description:
  142. * Marks the passed in sg entry as the termination point for the sg
  143. * table. A call to sg_next() on this entry will return NULL.
  144. *
  145. **/
  146. static inline void sg_mark_end(struct scatterlist *sg)
  147. {
  148. #ifdef CONFIG_DEBUG_SG
  149. BUG_ON(sg->sg_magic != SG_MAGIC);
  150. #endif
  151. /*
  152. * Set termination bit, clear potential chain bit
  153. */
  154. sg->page_link |= 0x02;
  155. sg->page_link &= ~0x01;
  156. }
  157. /**
  158. * sg_phys - Return physical address of an sg entry
  159. * @sg: SG entry
  160. *
  161. * Description:
  162. * This calls page_to_phys() on the page in this sg entry, and adds the
  163. * sg offset. The caller must know that it is legal to call page_to_phys()
  164. * on the sg page.
  165. *
  166. **/
  167. static inline dma_addr_t sg_phys(struct scatterlist *sg)
  168. {
  169. return page_to_phys(sg_page(sg)) + sg->offset;
  170. }
  171. /**
  172. * sg_virt - Return virtual address of an sg entry
  173. * @sg: SG entry
  174. *
  175. * Description:
  176. * This calls page_address() on the page in this sg entry, and adds the
  177. * sg offset. The caller must know that the sg page has a valid virtual
  178. * mapping.
  179. *
  180. **/
  181. static inline void *sg_virt(struct scatterlist *sg)
  182. {
  183. return page_address(sg_page(sg)) + sg->offset;
  184. }
  185. int sg_nents(struct scatterlist *sg);
  186. struct scatterlist *sg_next(struct scatterlist *);
  187. struct scatterlist *sg_last(struct scatterlist *s, unsigned int);
  188. void sg_init_table(struct scatterlist *, unsigned int);
  189. void sg_init_one(struct scatterlist *, const void *, unsigned int);
  190. typedef struct scatterlist *(sg_alloc_fn)(unsigned int, gfp_t);
  191. typedef void (sg_free_fn)(struct scatterlist *, unsigned int);
  192. void __sg_free_table(struct sg_table *, unsigned int, sg_free_fn *);
  193. void sg_free_table(struct sg_table *);
  194. int __sg_alloc_table(struct sg_table *, unsigned int, unsigned int, gfp_t,
  195. sg_alloc_fn *);
  196. int sg_alloc_table(struct sg_table *, unsigned int, gfp_t);
  197. int sg_alloc_table_from_pages(struct sg_table *sgt,
  198. struct page **pages, unsigned int n_pages,
  199. unsigned long offset, unsigned long size,
  200. gfp_t gfp_mask);
  201. size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
  202. void *buf, size_t buflen);
  203. size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
  204. void *buf, size_t buflen);
  205. /*
  206. * Maximum number of entries that will be allocated in one piece, if
  207. * a list larger than this is required then chaining will be utilized.
  208. */
  209. #define SG_MAX_SINGLE_ALLOC (PAGE_SIZE / sizeof(struct scatterlist))
  210. /*
  211. * sg page iterator
  212. *
  213. * Iterates over sg entries page-by-page. On each successful iteration,
  214. * @piter->page points to the current page, @piter->sg to the sg holding this
  215. * page and @piter->sg_pgoffset to the page's page offset within the sg. The
  216. * iteration will stop either when a maximum number of sg entries was reached
  217. * or a terminating sg (sg_last(sg) == true) was reached.
  218. */
  219. struct sg_page_iter {
  220. struct page *page; /* current page */
  221. struct scatterlist *sg; /* sg holding the page */
  222. unsigned int sg_pgoffset; /* page offset within the sg */
  223. /* these are internal states, keep away */
  224. unsigned int __nents; /* remaining sg entries */
  225. int __pg_advance; /* nr pages to advance at the
  226. * next step */
  227. };
  228. bool __sg_page_iter_next(struct sg_page_iter *piter);
  229. void __sg_page_iter_start(struct sg_page_iter *piter,
  230. struct scatterlist *sglist, unsigned int nents,
  231. unsigned long pgoffset);
  232. /**
  233. * for_each_sg_page - iterate over the pages of the given sg list
  234. * @sglist: sglist to iterate over
  235. * @piter: page iterator to hold current page, sg, sg_pgoffset
  236. * @nents: maximum number of sg entries to iterate over
  237. * @pgoffset: starting page offset
  238. */
  239. #define for_each_sg_page(sglist, piter, nents, pgoffset) \
  240. for (__sg_page_iter_start((piter), (sglist), (nents), (pgoffset)); \
  241. __sg_page_iter_next(piter);)
  242. /*
  243. * Mapping sg iterator
  244. *
  245. * Iterates over sg entries mapping page-by-page. On each successful
  246. * iteration, @miter->page points to the mapped page and
  247. * @miter->length bytes of data can be accessed at @miter->addr. As
  248. * long as an interation is enclosed between start and stop, the user
  249. * is free to choose control structure and when to stop.
  250. *
  251. * @miter->consumed is set to @miter->length on each iteration. It
  252. * can be adjusted if the user can't consume all the bytes in one go.
  253. * Also, a stopped iteration can be resumed by calling next on it.
  254. * This is useful when iteration needs to release all resources and
  255. * continue later (e.g. at the next interrupt).
  256. */
  257. #define SG_MITER_ATOMIC (1 << 0) /* use kmap_atomic */
  258. #define SG_MITER_TO_SG (1 << 1) /* flush back to phys on unmap */
  259. #define SG_MITER_FROM_SG (1 << 2) /* nop */
  260. struct sg_mapping_iter {
  261. /* the following three fields can be accessed directly */
  262. struct page *page; /* currently mapped page */
  263. void *addr; /* pointer to the mapped area */
  264. size_t length; /* length of the mapped area */
  265. size_t consumed; /* number of consumed bytes */
  266. /* these are internal states, keep away */
  267. struct scatterlist *__sg; /* current entry */
  268. unsigned int __nents; /* nr of remaining entries */
  269. unsigned int __offset; /* offset within sg */
  270. unsigned int __flags;
  271. };
  272. void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
  273. unsigned int nents, unsigned int flags);
  274. bool sg_miter_next(struct sg_mapping_iter *miter);
  275. void sg_miter_stop(struct sg_mapping_iter *miter);
  276. #endif /* _LINUX_SCATTERLIST_H */