1 #ifndef _LINUX_SCATTERLIST_H
2 #define _LINUX_SCATTERLIST_H
5 #include <linux/slab.h>
8 unsigned long page_link;
13 #define sg_is_chain(sg) ((sg)->page_link & 0x01)
14 #define sg_is_last(sg) ((sg)->page_link & 0x02)
15 #define sg_chain_ptr(sg) \
16 ((struct scatterlist *) ((sg)->page_link & ~0x03))
18 static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
20 unsigned long page_link = sg->page_link & 0x3;
23 * In order for the low bit stealing approach to work, pages
24 * must be aligned at a 32-bit boundary as a minimum.
26 BUG_ON((unsigned long) page & 0x03);
27 sg->page_link = page_link | (unsigned long) page;
30 static inline void sg_set_page(struct scatterlist *sg, struct page *page,
31 unsigned int len, unsigned int offset)
33 sg_assign_page(sg, page);
38 static inline struct page *sg_page(struct scatterlist *sg)
40 return (struct page *)((sg)->page_link & ~0x3);
43 static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
46 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
49 static inline struct scatterlist *sg_next(struct scatterlist *sg)
55 if (unlikely(sg_is_chain(sg)))
56 sg = sg_chain_ptr(sg);
61 #define for_each_sg(sglist, sg, nr, __i) \
62 for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
64 static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents,
65 struct scatterlist *sgl)
68 * offset and length are unused for chain entry. Clear them.
70 prv[prv_nents - 1].offset = 0;
71 prv[prv_nents - 1].length = 0;
74 * Set lowest bit to indicate a link pointer, and make sure to clear
75 * the termination bit if it happens to be set.
77 prv[prv_nents - 1].page_link = ((unsigned long) sgl | 0x01) & ~0x02;
80 static inline void sg_mark_end(struct scatterlist *sg)
82 sg->page_link |= 0x02;
83 sg->page_link &= ~0x01;
86 static inline void sg_unmark_end(struct scatterlist *sg)
88 sg->page_link &= ~0x02;
91 static inline void *sg_virt(struct scatterlist *sg)
93 return page_address(sg_page(sg)) + sg->offset;
96 static inline void sg_init_table(struct scatterlist *sgl, unsigned int nents)
98 memset(sgl, 0, sizeof(*sgl) * nents);
99 sg_mark_end(&sgl[nents - 1]);
102 static inline void sg_init_one(struct scatterlist *sg, const void *buf,
105 sg_init_table(sg, 1);
106 sg_set_buf(sg, buf, buflen);
109 #endif /* _LINUX_SCATTERLIST_H */