scatterlist.h 7.6 KB

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