1 #ifndef _LINUX_SCATTERLIST_H
2 #define _LINUX_SCATTERLIST_H
5 #include <asm/scatterlist.h>
7 #include <linux/string.h>
11 * Notes on SG table design.
13 * Architectures must provide an unsigned long page_link field in the
14 * scatterlist struct. We use that to place the page pointer AND encode
15 * information about the sg table as well. The two lower bits are reserved
16 * for this information.
18 * If bit 0 is set, then the page_link contains a pointer to the next sg
19 * table list. Otherwise the next entry is at sg + 1.
21 * If bit 1 is set, then this sg entry is the last element in a list.
27 #define SG_MAGIC 0x87654321
30 * We overload the LSB of the page pointer to indicate whether it's
31 * a valid sg entry, or whether it points to the start of a new scatterlist.
32 * Those low bits are there for everyone! (thanks mason :-)
34 #define sg_is_chain(sg) ((sg)->page_link & 0x01)
35 #define sg_is_last(sg) ((sg)->page_link & 0x02)
36 #define sg_chain_ptr(sg) \
37 ((struct scatterlist *) ((sg)->page_link & ~0x03))
40 * sg_assign_page - Assign a given page to an SG entry
45 * Assign page to sg entry. Also see sg_set_page(), the most commonly used
49 static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
51 unsigned long page_link = sg->page_link & 0x3;
54 * In order for the low bit stealing approach to work, pages
55 * must be aligned at a 32-bit boundary as a minimum.
57 BUG_ON((unsigned long) page & 0x03);
58 #ifdef CONFIG_DEBUG_SG
59 BUG_ON(sg->sg_magic != SG_MAGIC);
60 BUG_ON(sg_is_chain(sg));
62 sg->page_link = page_link | (unsigned long) page;
66 * sg_set_page - Set sg entry to point at given page
69 * @len: Length of data
70 * @offset: Offset into page
73 * Use this function to set an sg entry pointing at a page, never assign
74 * the page directly. We encode sg table information in the lower bits
75 * of the page pointer. See sg_page() for looking up the page belonging
79 static inline void sg_set_page(struct scatterlist *sg, struct page *page,
80 unsigned int len, unsigned int offset)
82 sg_assign_page(sg, page);
87 static inline struct page *sg_page(struct scatterlist *sg)
89 #ifdef CONFIG_DEBUG_SG
90 BUG_ON(sg->sg_magic != SG_MAGIC);
91 BUG_ON(sg_is_chain(sg));
93 return (struct page *)((sg)->page_link & ~0x3);
97 * sg_set_buf - Set sg entry to point at given data
100 * @buflen: Data length
103 static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
106 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
110 * sg_next - return the next scatterlist entry in a list
111 * @sg: The current sg entry
114 * Usually the next entry will be @sg@ + 1, but if this sg element is part
115 * of a chained scatterlist, it could jump to the start of a new
119 static inline struct scatterlist *sg_next(struct scatterlist *sg)
121 #ifdef CONFIG_DEBUG_SG
122 BUG_ON(sg->sg_magic != SG_MAGIC);
128 if (unlikely(sg_is_chain(sg)))
129 sg = sg_chain_ptr(sg);
135 * Loop over each sg element, following the pointer to a new list if necessary
137 #define for_each_sg(sglist, sg, nr, __i) \
138 for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
141 * sg_last - return the last scatterlist entry in a list
142 * @sgl: First entry in the scatterlist
143 * @nents: Number of entries in the scatterlist
146 * Should only be used casually, it (currently) scan the entire list
147 * to get the last entry.
149 * Note that the @sgl@ pointer passed in need not be the first one,
150 * the important bit is that @nents@ denotes the number of entries that
154 static inline struct scatterlist *sg_last(struct scatterlist *sgl,
157 #ifndef ARCH_HAS_SG_CHAIN
158 struct scatterlist *ret = &sgl[nents - 1];
160 struct scatterlist *sg, *ret = NULL;
163 for_each_sg(sgl, sg, nents, i)
167 #ifdef CONFIG_DEBUG_SG
168 BUG_ON(sgl[0].sg_magic != SG_MAGIC);
169 BUG_ON(!sg_is_last(ret));
175 * sg_chain - Chain two sglists together
176 * @prv: First scatterlist
177 * @prv_nents: Number of entries in prv
178 * @sgl: Second scatterlist
181 * Links @prv@ and @sgl@ together, to form a longer scatterlist.
184 static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents,
185 struct scatterlist *sgl)
187 #ifndef ARCH_HAS_SG_CHAIN
192 * offset and length are unused for chain entry. Clear them.
194 prv[prv_nents - 1].offset = 0;
195 prv[prv_nents - 1].length = 0;
198 * Set lowest bit to indicate a link pointer, and make sure to clear
199 * the termination bit if it happens to be set.
201 prv[prv_nents - 1].page_link = ((unsigned long) sgl | 0x01) & ~0x02;
205 * sg_mark_end - Mark the end of the scatterlist
206 * @sg: SG entryScatterlist
209 * Marks the passed in sg entry as the termination point for the sg
210 * table. A call to sg_next() on this entry will return NULL.
213 static inline void sg_mark_end(struct scatterlist *sg)
215 #ifdef CONFIG_DEBUG_SG
216 BUG_ON(sg->sg_magic != SG_MAGIC);
219 * Set termination bit, clear potential chain bit
221 sg->page_link |= 0x02;
222 sg->page_link &= ~0x01;
226 * sg_init_table - Initialize SG table
228 * @nents: Number of entries in table
231 * If this is part of a chained sg table, sg_mark_end() should be
232 * used only on the last table part.
235 static inline void sg_init_table(struct scatterlist *sgl, unsigned int nents)
237 memset(sgl, 0, sizeof(*sgl) * nents);
238 #ifdef CONFIG_DEBUG_SG
241 for (i = 0; i < nents; i++)
242 sgl[i].sg_magic = SG_MAGIC;
245 sg_mark_end(&sgl[nents - 1]);
249 * sg_init_one - Initialize a single entry sg list
251 * @buf: Virtual address for IO
255 * This should not be used on a single entry that is part of a larger
256 * table. Use sg_init_table() for that.
259 static inline void sg_init_one(struct scatterlist *sg, const void *buf,
262 sg_init_table(sg, 1);
263 sg_set_buf(sg, buf, buflen);
267 * sg_phys - Return physical address of an sg entry
271 * This calls page_to_phys() on the page in this sg entry, and adds the
272 * sg offset. The caller must know that it is legal to call page_to_phys()
276 static inline dma_addr_t sg_phys(struct scatterlist *sg)
278 return page_to_phys(sg_page(sg)) + sg->offset;
282 * sg_virt - Return virtual address of an sg entry
286 * This calls page_address() on the page in this sg entry, and adds the
287 * sg offset. The caller must know that the sg page has a valid virtual
291 static inline void *sg_virt(struct scatterlist *sg)
293 return page_address(sg_page(sg)) + sg->offset;
296 #endif /* _LINUX_SCATTERLIST_H */