1 #ifndef _LINUX_SCATTERLIST_H
2 #define _LINUX_SCATTERLIST_H
4 #include <asm/scatterlist.h>
6 #include <linux/string.h>
8 static inline void sg_set_buf(struct scatterlist
*sg
, const void *buf
,
11 sg
->page
= virt_to_page(buf
);
12 sg
->offset
= offset_in_page(buf
);
16 static inline void sg_init_one(struct scatterlist
*sg
, const void *buf
,
19 memset(sg
, 0, sizeof(*sg
));
20 sg_set_buf(sg
, buf
, buflen
);
24 * We overload the LSB of the page pointer to indicate whether it's
25 * a valid sg entry, or whether it points to the start of a new scatterlist.
26 * Those low bits are there for everyone! (thanks mason :-)
28 #define sg_is_chain(sg) ((unsigned long) (sg)->page & 0x01)
29 #define sg_chain_ptr(sg) \
30 ((struct scatterlist *) ((unsigned long) (sg)->page & ~0x01))
33 * sg_next - return the next scatterlist entry in a list
34 * @sg: The current sg entry
36 * Usually the next entry will be @sg@ + 1, but if this sg element is part
37 * of a chained scatterlist, it could jump to the start of a new
40 * Note that the caller must ensure that there are further entries after
41 * the current entry, this function will NOT return NULL for an end-of-list.
44 static inline struct scatterlist
*sg_next(struct scatterlist
*sg
)
48 if (unlikely(sg_is_chain(sg
)))
49 sg
= sg_chain_ptr(sg
);
55 * Loop over each sg element, following the pointer to a new list if necessary
57 #define for_each_sg(sglist, sg, nr, __i) \
58 for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
61 * sg_last - return the last scatterlist entry in a list
62 * @sgl: First entry in the scatterlist
63 * @nents: Number of entries in the scatterlist
65 * Should only be used casually, it (currently) scan the entire list
66 * to get the last entry.
68 * Note that the @sgl@ pointer passed in need not be the first one,
69 * the important bit is that @nents@ denotes the number of entries that
73 static inline struct scatterlist
*sg_last(struct scatterlist
*sgl
,
76 #ifndef ARCH_HAS_SG_CHAIN
77 struct scatterlist
*ret
= &sgl
[nents
- 1];
79 struct scatterlist
*sg
, *ret
= NULL
;
82 for_each_sg(sgl
, sg
, nents
, i
)
90 * sg_chain - Chain two sglists together
91 * @prv: First scatterlist
92 * @prv_nents: Number of entries in prv
93 * @sgl: Second scatterlist
95 * Links @prv@ and @sgl@ together, to form a longer scatterlist.
98 static inline void sg_chain(struct scatterlist
*prv
, unsigned int prv_nents
,
99 struct scatterlist
*sgl
)
101 #ifndef ARCH_HAS_SG_CHAIN
104 prv
[prv_nents
- 1].page
= (struct page
*) ((unsigned long) sgl
| 0x01);
107 #endif /* _LINUX_SCATTERLIST_H */