scatterlist.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. /*
  9. * Notes on SG table design.
  10. *
  11. * Architectures must provide an unsigned long page_link field in the
  12. * scatterlist struct. We use that to place the page pointer AND encode
  13. * information about the sg table as well. The two lower bits are reserved
  14. * for this information.
  15. *
  16. * If bit 0 is set, then the page_link contains a pointer to the next sg
  17. * table list. Otherwise the next entry is at sg + 1.
  18. *
  19. * If bit 1 is set, then this sg entry is the last element in a list.
  20. *
  21. * See sg_next().
  22. *
  23. */
  24. #define SG_MAGIC 0x87654321
  25. /**
  26. * sg_assign_page - Assign a given page to an SG entry
  27. * @sg: SG entry
  28. * @page: The page
  29. *
  30. * Description:
  31. * Assign page to sg entry. Also see sg_set_page(), the most commonly used
  32. * variant.
  33. *
  34. **/
  35. static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
  36. {
  37. unsigned long page_link = sg->page_link & 0x3;
  38. /*
  39. * In order for the low bit stealing approach to work, pages
  40. * must be aligned at a 32-bit boundary as a minimum.
  41. */
  42. BUG_ON((unsigned long) page & 0x03);
  43. #ifdef CONFIG_DEBUG_SG
  44. BUG_ON(sg->sg_magic != SG_MAGIC);
  45. #endif
  46. sg->page_link = page_link | (unsigned long) page;
  47. }
  48. /**
  49. * sg_set_page - Set sg entry to point at given page
  50. * @sg: SG entry
  51. * @page: The page
  52. * @len: Length of data
  53. * @offset: Offset into page
  54. *
  55. * Description:
  56. * Use this function to set an sg entry pointing at a page, never assign
  57. * the page directly. We encode sg table information in the lower bits
  58. * of the page pointer. See sg_page() for looking up the page belonging
  59. * to an sg entry.
  60. *
  61. **/
  62. static inline void sg_set_page(struct scatterlist *sg, struct page *page,
  63. unsigned int len, unsigned int offset)
  64. {
  65. sg_assign_page(sg, page);
  66. sg->offset = offset;
  67. sg->length = len;
  68. }
  69. #define sg_page(sg) ((struct page *) ((sg)->page_link & ~0x3))
  70. /**
  71. * sg_set_buf - Set sg entry to point at given data
  72. * @sg: SG entry
  73. * @buf: Data
  74. * @buflen: Data length
  75. *
  76. **/
  77. static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
  78. unsigned int buflen)
  79. {
  80. sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
  81. }
  82. /*
  83. * We overload the LSB of the page pointer to indicate whether it's
  84. * a valid sg entry, or whether it points to the start of a new scatterlist.
  85. * Those low bits are there for everyone! (thanks mason :-)
  86. */
  87. #define sg_is_chain(sg) ((sg)->page_link & 0x01)
  88. #define sg_is_last(sg) ((sg)->page_link & 0x02)
  89. #define sg_chain_ptr(sg) \
  90. ((struct scatterlist *) ((sg)->page_link & ~0x03))
  91. /**
  92. * sg_next - return the next scatterlist entry in a list
  93. * @sg: The current sg entry
  94. *
  95. * Description:
  96. * Usually the next entry will be @sg@ + 1, but if this sg element is part
  97. * of a chained scatterlist, it could jump to the start of a new
  98. * scatterlist array.
  99. *
  100. **/
  101. static inline struct scatterlist *sg_next(struct scatterlist *sg)
  102. {
  103. #ifdef CONFIG_DEBUG_SG
  104. BUG_ON(sg->sg_magic != SG_MAGIC);
  105. #endif
  106. if (sg_is_last(sg))
  107. return NULL;
  108. sg++;
  109. if (unlikely(sg_is_chain(sg)))
  110. sg = sg_chain_ptr(sg);
  111. return sg;
  112. }
  113. /*
  114. * Loop over each sg element, following the pointer to a new list if necessary
  115. */
  116. #define for_each_sg(sglist, sg, nr, __i) \
  117. for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
  118. /**
  119. * sg_last - return the last scatterlist entry in a list
  120. * @sgl: First entry in the scatterlist
  121. * @nents: Number of entries in the scatterlist
  122. *
  123. * Description:
  124. * Should only be used casually, it (currently) scan the entire list
  125. * to get the last entry.
  126. *
  127. * Note that the @sgl@ pointer passed in need not be the first one,
  128. * the important bit is that @nents@ denotes the number of entries that
  129. * exist from @sgl@.
  130. *
  131. **/
  132. static inline struct scatterlist *sg_last(struct scatterlist *sgl,
  133. unsigned int nents)
  134. {
  135. #ifndef ARCH_HAS_SG_CHAIN
  136. struct scatterlist *ret = &sgl[nents - 1];
  137. #else
  138. struct scatterlist *sg, *ret = NULL;
  139. unsigned int i;
  140. for_each_sg(sgl, sg, nents, i)
  141. ret = sg;
  142. #endif
  143. #ifdef CONFIG_DEBUG_SG
  144. BUG_ON(sgl[0].sg_magic != SG_MAGIC);
  145. BUG_ON(!sg_is_last(ret));
  146. #endif
  147. return ret;
  148. }
  149. /**
  150. * sg_chain - Chain two sglists together
  151. * @prv: First scatterlist
  152. * @prv_nents: Number of entries in prv
  153. * @sgl: Second scatterlist
  154. *
  155. * Description:
  156. * Links @prv@ and @sgl@ together, to form a longer scatterlist.
  157. *
  158. **/
  159. static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents,
  160. struct scatterlist *sgl)
  161. {
  162. #ifndef ARCH_HAS_SG_CHAIN
  163. BUG();
  164. #endif
  165. /*
  166. * Set lowest bit to indicate a link pointer, and make sure to clear
  167. * the termination bit if it happens to be set.
  168. */
  169. prv[prv_nents - 1].page_link = ((unsigned long) sgl | 0x01) & ~0x02;
  170. }
  171. /**
  172. * sg_mark_end - Mark the end of the scatterlist
  173. * @sg: SG entryScatterlist
  174. *
  175. * Description:
  176. * Marks the passed in sg entry as the termination point for the sg
  177. * table. A call to sg_next() on this entry will return NULL.
  178. *
  179. **/
  180. static inline void sg_mark_end(struct scatterlist *sg)
  181. {
  182. #ifdef CONFIG_DEBUG_SG
  183. BUG_ON(sg->sg_magic != SG_MAGIC);
  184. #endif
  185. /*
  186. * Set termination bit, clear potential chain bit
  187. */
  188. sg->page_link |= 0x02;
  189. sg->page_link &= ~0x01;
  190. }
  191. /**
  192. * sg_init_table - Initialize SG table
  193. * @sgl: The SG table
  194. * @nents: Number of entries in table
  195. *
  196. * Notes:
  197. * If this is part of a chained sg table, sg_mark_end() should be
  198. * used only on the last table part.
  199. *
  200. **/
  201. static inline void sg_init_table(struct scatterlist *sgl, unsigned int nents)
  202. {
  203. memset(sgl, 0, sizeof(*sgl) * nents);
  204. #ifdef CONFIG_DEBUG_SG
  205. {
  206. unsigned int i;
  207. for (i = 0; i < nents; i++)
  208. sgl[i].sg_magic = SG_MAGIC;
  209. }
  210. #endif
  211. sg_mark_end(&sgl[nents - 1]);
  212. }
  213. /**
  214. * sg_init_one - Initialize a single entry sg list
  215. * @sg: SG entry
  216. * @buf: Virtual address for IO
  217. * @buflen: IO length
  218. *
  219. * Notes:
  220. * This should not be used on a single entry that is part of a larger
  221. * table. Use sg_init_table() for that.
  222. *
  223. **/
  224. static inline void sg_init_one(struct scatterlist *sg, const void *buf,
  225. unsigned int buflen)
  226. {
  227. sg_init_table(sg, 1);
  228. sg_set_buf(sg, buf, buflen);
  229. }
  230. /**
  231. * sg_phys - Return physical address of an sg entry
  232. * @sg: SG entry
  233. *
  234. * Description:
  235. * This calls page_to_phys() on the page in this sg entry, and adds the
  236. * sg offset. The caller must know that it is legal to call page_to_phys()
  237. * on the sg page.
  238. *
  239. **/
  240. static inline dma_addr_t sg_phys(struct scatterlist *sg)
  241. {
  242. return page_to_phys(sg_page(sg)) + sg->offset;
  243. }
  244. /**
  245. * sg_virt - Return virtual address of an sg entry
  246. * @sg: SG entry
  247. *
  248. * Description:
  249. * This calls page_address() on the page in this sg entry, and adds the
  250. * sg offset. The caller must know that the sg page has a valid virtual
  251. * mapping.
  252. *
  253. **/
  254. static inline void *sg_virt(struct scatterlist *sg)
  255. {
  256. return page_address(sg_page(sg)) + sg->offset;
  257. }
  258. #endif /* _LINUX_SCATTERLIST_H */