1 #ifndef _LINUX_SCATTERLIST_H
2 #define _LINUX_SCATTERLIST_H
4 #include <asm/scatterlist.h>
6 #include <linux/string.h>
10 * Notes on SG table design.
12 * Architectures must provide an unsigned long page_link field in the
13 * scatterlist struct. We use that to place the page pointer AND encode
14 * information about the sg table as well. The two lower bits are reserved
15 * for this information.
17 * If bit 0 is set, then the page_link contains a pointer to the next sg
18 * table list. Otherwise the next entry is at sg + 1.
20 * If bit 1 is set, then this sg entry is the last element in a list.
26 #define SG_MAGIC 0x87654321
29 * sg_set_page - Set sg entry to point at given page
34 * Use this function to set an sg entry pointing at a page, never assign
35 * the page directly. We encode sg table information in the lower bits
36 * of the page pointer. See sg_page() for looking up the page belonging
40 static inline void sg_set_page(struct scatterlist *sg, struct page *page)
42 unsigned long page_link = sg->page_link & 0x3;
44 #ifdef CONFIG_DEBUG_SG
45 BUG_ON(sg->sg_magic != SG_MAGIC);
47 sg->page_link = page_link | (unsigned long) page;
50 #define sg_page(sg) ((struct page *) ((sg)->page_link & ~0x3))
53 * sg_set_buf - Set sg entry to point at given data
56 * @buflen: Data length
59 static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
62 sg_set_page(sg, virt_to_page(buf));
63 sg->offset = offset_in_page(buf);
68 * We overload the LSB of the page pointer to indicate whether it's
69 * a valid sg entry, or whether it points to the start of a new scatterlist.
70 * Those low bits are there for everyone! (thanks mason :-)
72 #define sg_is_chain(sg) ((sg)->page_link & 0x01)
73 #define sg_is_last(sg) ((sg)->page_link & 0x02)
74 #define sg_chain_ptr(sg) \
75 ((struct scatterlist *) ((sg)->page_link & ~0x03))
78 * sg_next - return the next scatterlist entry in a list
79 * @sg: The current sg entry
82 * Usually the next entry will be @sg@ + 1, but if this sg element is part
83 * of a chained scatterlist, it could jump to the start of a new
87 static inline struct scatterlist *sg_next(struct scatterlist *sg)
89 #ifdef CONFIG_DEBUG_SG
90 BUG_ON(sg->sg_magic != SG_MAGIC);
96 if (unlikely(sg_is_chain(sg)))
97 sg = sg_chain_ptr(sg);
103 * Loop over each sg element, following the pointer to a new list if necessary
105 #define for_each_sg(sglist, sg, nr, __i) \
106 for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
109 * sg_last - return the last scatterlist entry in a list
110 * @sgl: First entry in the scatterlist
111 * @nents: Number of entries in the scatterlist
114 * Should only be used casually, it (currently) scan the entire list
115 * to get the last entry.
117 * Note that the @sgl@ pointer passed in need not be the first one,
118 * the important bit is that @nents@ denotes the number of entries that
122 static inline struct scatterlist *sg_last(struct scatterlist *sgl,
125 #ifndef ARCH_HAS_SG_CHAIN
126 struct scatterlist *ret = &sgl[nents - 1];
128 struct scatterlist *sg, *ret = NULL;
131 for_each_sg(sgl, sg, nents, i)
135 #ifdef CONFIG_DEBUG_SG
136 BUG_ON(sgl[0].sg_magic != SG_MAGIC);
137 BUG_ON(!sg_is_last(ret));
143 * sg_chain - Chain two sglists together
144 * @prv: First scatterlist
145 * @prv_nents: Number of entries in prv
146 * @sgl: Second scatterlist
149 * Links @prv@ and @sgl@ together, to form a longer scatterlist.
152 static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents,
153 struct scatterlist *sgl)
155 #ifndef ARCH_HAS_SG_CHAIN
158 prv[prv_nents - 1].page_link = (unsigned long) sgl | 0x01;
162 * sg_mark_end - Mark the end of the scatterlist
164 * @nents: Number of entries in sgl
167 * Marks the last entry as the termination point for sg_next()
170 static inline void sg_mark_end(struct scatterlist *sgl, unsigned int nents)
172 sgl[nents - 1].page_link = 0x02;
175 static inline void __sg_mark_end(struct scatterlist *sg)
177 sg->page_link |= 0x02;
181 * sg_init_one - Initialize a single entry sg list
183 * @buf: Virtual address for IO
187 * This should not be used on a single entry that is part of a larger
188 * table. Use sg_init_table() for that.
191 static inline void sg_init_one(struct scatterlist *sg, const void *buf,
194 memset(sg, 0, sizeof(*sg));
195 #ifdef CONFIG_DEBUG_SG
196 sg->sg_magic = SG_MAGIC;
199 sg_set_buf(sg, buf, buflen);
203 * sg_init_table - Initialize SG table
205 * @nents: Number of entries in table
208 * If this is part of a chained sg table, sg_mark_end() should be
209 * used only on the last table part.
212 static inline void sg_init_table(struct scatterlist *sgl, unsigned int nents)
214 memset(sgl, 0, sizeof(*sgl) * nents);
215 sg_mark_end(sgl, nents);
216 #ifdef CONFIG_DEBUG_SG
219 for (i = 0; i < nents; i++)
220 sgl[i].sg_magic = SG_MAGIC;
226 * sg_phys - Return physical address of an sg entry
230 * This calls page_to_phys() on the page in this sg entry, and adds the
231 * sg offset. The caller must know that it is legal to call page_to_phys()
235 static inline unsigned long sg_phys(struct scatterlist *sg)
237 return page_to_phys(sg_page(sg)) + sg->offset;
241 * sg_virt - Return virtual address of an sg entry
245 * This calls page_address() on the page in this sg entry, and adds the
246 * sg offset. The caller must know that the sg page has a valid virtual
250 static inline void *sg_virt(struct scatterlist *sg)
252 return page_address(sg_page(sg)) + sg->offset;
255 #endif /* _LINUX_SCATTERLIST_H */