scatterlist.h 7.2 KB

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