scatterlist.h 6.1 KB

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