scatterlist.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. struct scatterlist *sg_next(struct scatterlist *);
  186. struct scatterlist *sg_last(struct scatterlist *s, unsigned int);
  187. void sg_init_table(struct scatterlist *, unsigned int);
  188. void sg_init_one(struct scatterlist *, const void *, unsigned int);
  189. typedef struct scatterlist *(sg_alloc_fn)(unsigned int, gfp_t);
  190. typedef void (sg_free_fn)(struct scatterlist *, unsigned int);
  191. void __sg_free_table(struct sg_table *, unsigned int, sg_free_fn *);
  192. void sg_free_table(struct sg_table *);
  193. int __sg_alloc_table(struct sg_table *, unsigned int, unsigned int, gfp_t,
  194. sg_alloc_fn *);
  195. int sg_alloc_table(struct sg_table *, unsigned int, gfp_t);
  196. size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
  197. void *buf, size_t buflen);
  198. size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
  199. void *buf, size_t buflen);
  200. /*
  201. * Maximum number of entries that will be allocated in one piece, if
  202. * a list larger than this is required then chaining will be utilized.
  203. */
  204. #define SG_MAX_SINGLE_ALLOC (PAGE_SIZE / sizeof(struct scatterlist))
  205. /*
  206. * Mapping sg iterator
  207. *
  208. * Iterates over sg entries mapping page-by-page. On each successful
  209. * iteration, @miter->page points to the mapped page and
  210. * @miter->length bytes of data can be accessed at @miter->addr. As
  211. * long as an interation is enclosed between start and stop, the user
  212. * is free to choose control structure and when to stop.
  213. *
  214. * @miter->consumed is set to @miter->length on each iteration. It
  215. * can be adjusted if the user can't consume all the bytes in one go.
  216. * Also, a stopped iteration can be resumed by calling next on it.
  217. * This is useful when iteration needs to release all resources and
  218. * continue later (e.g. at the next interrupt).
  219. */
  220. #define SG_MITER_ATOMIC (1 << 0) /* use kmap_atomic */
  221. #define SG_MITER_TO_SG (1 << 1) /* flush back to phys on unmap */
  222. #define SG_MITER_FROM_SG (1 << 2) /* nop */
  223. struct sg_mapping_iter {
  224. /* the following three fields can be accessed directly */
  225. struct page *page; /* currently mapped page */
  226. void *addr; /* pointer to the mapped area */
  227. size_t length; /* length of the mapped area */
  228. size_t consumed; /* number of consumed bytes */
  229. /* these are internal states, keep away */
  230. struct scatterlist *__sg; /* current entry */
  231. unsigned int __nents; /* nr of remaining entries */
  232. unsigned int __offset; /* offset within sg */
  233. unsigned int __flags;
  234. };
  235. void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
  236. unsigned int nents, unsigned int flags);
  237. bool sg_miter_next(struct sg_mapping_iter *miter);
  238. void sg_miter_stop(struct sg_mapping_iter *miter);
  239. #endif /* _LINUX_SCATTERLIST_H */