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 * sg_assign_page - Assign a given page to an SG entry
35 * Assign page to sg entry. Also see sg_set_page(), the most commonly used
39 static inline void sg_assign_page(struct scatterlist
*sg
, struct page
*page
)
41 unsigned long page_link
= sg
->page_link
& 0x3;
44 * In order for the low bit stealing approach to work, pages
45 * must be aligned at a 32-bit boundary as a minimum.
47 BUG_ON((unsigned long) page
& 0x03);
48 #ifdef CONFIG_DEBUG_SG
49 BUG_ON(sg
->sg_magic
!= SG_MAGIC
);
51 sg
->page_link
= page_link
| (unsigned long) page
;
55 * sg_set_page - Set sg entry to point at given page
58 * @len: Length of data
59 * @offset: Offset into page
62 * Use this function to set an sg entry pointing at a page, never assign
63 * the page directly. We encode sg table information in the lower bits
64 * of the page pointer. See sg_page() for looking up the page belonging
68 static inline void sg_set_page(struct scatterlist
*sg
, struct page
*page
,
69 unsigned int len
, unsigned int offset
)
71 sg_assign_page(sg
, page
);
76 #define sg_page(sg) ((struct page *) ((sg)->page_link & ~0x3))
79 * sg_set_buf - Set sg entry to point at given data
82 * @buflen: Data length
85 static inline void sg_set_buf(struct scatterlist
*sg
, const void *buf
,
88 sg_set_page(sg
, virt_to_page(buf
), buflen
, offset_in_page(buf
));
92 * We overload the LSB of the page pointer to indicate whether it's
93 * a valid sg entry, or whether it points to the start of a new scatterlist.
94 * Those low bits are there for everyone! (thanks mason :-)
96 #define sg_is_chain(sg) ((sg)->page_link & 0x01)
97 #define sg_is_last(sg) ((sg)->page_link & 0x02)
98 #define sg_chain_ptr(sg) \
99 ((struct scatterlist *) ((sg)->page_link & ~0x03))
102 * sg_next - return the next scatterlist entry in a list
103 * @sg: The current sg entry
106 * Usually the next entry will be @sg@ + 1, but if this sg element is part
107 * of a chained scatterlist, it could jump to the start of a new
111 static inline struct scatterlist
*sg_next(struct scatterlist
*sg
)
113 #ifdef CONFIG_DEBUG_SG
114 BUG_ON(sg
->sg_magic
!= SG_MAGIC
);
120 if (unlikely(sg_is_chain(sg
)))
121 sg
= sg_chain_ptr(sg
);
127 * Loop over each sg element, following the pointer to a new list if necessary
129 #define for_each_sg(sglist, sg, nr, __i) \
130 for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
133 * sg_last - return the last scatterlist entry in a list
134 * @sgl: First entry in the scatterlist
135 * @nents: Number of entries in the scatterlist
138 * Should only be used casually, it (currently) scan the entire list
139 * to get the last entry.
141 * Note that the @sgl@ pointer passed in need not be the first one,
142 * the important bit is that @nents@ denotes the number of entries that
146 static inline struct scatterlist
*sg_last(struct scatterlist
*sgl
,
149 #ifndef ARCH_HAS_SG_CHAIN
150 struct scatterlist
*ret
= &sgl
[nents
- 1];
152 struct scatterlist
*sg
, *ret
= NULL
;
155 for_each_sg(sgl
, sg
, nents
, i
)
159 #ifdef CONFIG_DEBUG_SG
160 BUG_ON(sgl
[0].sg_magic
!= SG_MAGIC
);
161 BUG_ON(!sg_is_last(ret
));
167 * sg_chain - Chain two sglists together
168 * @prv: First scatterlist
169 * @prv_nents: Number of entries in prv
170 * @sgl: Second scatterlist
173 * Links @prv@ and @sgl@ together, to form a longer scatterlist.
176 static inline void sg_chain(struct scatterlist
*prv
, unsigned int prv_nents
,
177 struct scatterlist
*sgl
)
179 #ifndef ARCH_HAS_SG_CHAIN
183 * Set lowest bit to indicate a link pointer, and make sure to clear
184 * the termination bit if it happens to be set.
186 prv
[prv_nents
- 1].page_link
= ((unsigned long) sgl
| 0x01) & ~0x02;
190 * sg_mark_end - Mark the end of the scatterlist
192 * @nents: Number of entries in sgl
195 * Marks the last entry as the termination point for sg_next()
198 static inline void sg_mark_end(struct scatterlist
*sgl
, unsigned int nents
)
200 sgl
[nents
- 1].page_link
= 0x02;
203 static inline void __sg_mark_end(struct scatterlist
*sg
)
205 sg
->page_link
|= 0x02;
209 * sg_init_one - Initialize a single entry sg list
211 * @buf: Virtual address for IO
215 * This should not be used on a single entry that is part of a larger
216 * table. Use sg_init_table() for that.
219 static inline void sg_init_one(struct scatterlist
*sg
, const void *buf
,
222 memset(sg
, 0, sizeof(*sg
));
223 #ifdef CONFIG_DEBUG_SG
224 sg
->sg_magic
= SG_MAGIC
;
227 sg_set_buf(sg
, buf
, buflen
);
231 * sg_init_table - Initialize SG table
233 * @nents: Number of entries in table
236 * If this is part of a chained sg table, sg_mark_end() should be
237 * used only on the last table part.
240 static inline void sg_init_table(struct scatterlist
*sgl
, unsigned int nents
)
242 memset(sgl
, 0, sizeof(*sgl
) * nents
);
243 sg_mark_end(sgl
, nents
);
244 #ifdef CONFIG_DEBUG_SG
247 for (i
= 0; i
< nents
; i
++)
248 sgl
[i
].sg_magic
= SG_MAGIC
;
254 * sg_phys - Return physical address of an sg entry
258 * This calls page_to_phys() on the page in this sg entry, and adds the
259 * sg offset. The caller must know that it is legal to call page_to_phys()
263 static inline dma_addr_t
sg_phys(struct scatterlist
*sg
)
265 return page_to_phys(sg_page(sg
)) + sg
->offset
;
269 * sg_virt - Return virtual address of an sg entry
273 * This calls page_address() on the page in this sg entry, and adds the
274 * sg offset. The caller must know that the sg page has a valid virtual
278 static inline void *sg_virt(struct scatterlist
*sg
)
280 return page_address(sg_page(sg
)) + sg
->offset
;
283 #endif /* _LINUX_SCATTERLIST_H */