]> git.karo-electronics.de Git - mv-sheeva.git/blob - include/linux/scatterlist.h
01779961c40c979e8541a48c098650f03f1521fd
[mv-sheeva.git] / include / linux / scatterlist.h
1 #ifndef _LINUX_SCATTERLIST_H
2 #define _LINUX_SCATTERLIST_H
3
4 #include <asm/types.h>
5 #include <asm/scatterlist.h>
6 #include <asm/kmap_types.h>
7 #include <linux/mm.h>
8 #include <linux/string.h>
9 #include <asm/io.h>
10
11 struct sg_table {
12         struct scatterlist *sgl;        /* the list */
13         unsigned int nents;             /* number of mapped entries */
14         unsigned int orig_nents;        /* original size of list */
15 };
16
17 /*
18  * Notes on SG table design.
19  *
20  * Architectures must provide an unsigned long page_link field in the
21  * scatterlist struct. We use that to place the page pointer AND encode
22  * information about the sg table as well. The two lower bits are reserved
23  * for this information.
24  *
25  * If bit 0 is set, then the page_link contains a pointer to the next sg
26  * table list. Otherwise the next entry is at sg + 1.
27  *
28  * If bit 1 is set, then this sg entry is the last element in a list.
29  *
30  * See sg_next().
31  *
32  */
33
34 #define SG_MAGIC        0x87654321
35
36 /*
37  * We overload the LSB of the page pointer to indicate whether it's
38  * a valid sg entry, or whether it points to the start of a new scatterlist.
39  * Those low bits are there for everyone! (thanks mason :-)
40  */
41 #define sg_is_chain(sg)         ((sg)->page_link & 0x01)
42 #define sg_is_last(sg)          ((sg)->page_link & 0x02)
43 #define sg_chain_ptr(sg)        \
44         ((struct scatterlist *) ((sg)->page_link & ~0x03))
45
46 /**
47  * sg_assign_page - Assign a given page to an SG entry
48  * @sg:             SG entry
49  * @page:           The page
50  *
51  * Description:
52  *   Assign page to sg entry. Also see sg_set_page(), the most commonly used
53  *   variant.
54  *
55  **/
56 static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
57 {
58         unsigned long page_link = sg->page_link & 0x3;
59
60         /*
61          * In order for the low bit stealing approach to work, pages
62          * must be aligned at a 32-bit boundary as a minimum.
63          */
64         BUG_ON((unsigned long) page & 0x03);
65 #ifdef CONFIG_DEBUG_SG
66         BUG_ON(sg->sg_magic != SG_MAGIC);
67         BUG_ON(sg_is_chain(sg));
68 #endif
69         sg->page_link = page_link | (unsigned long) page;
70 }
71
72 /**
73  * sg_set_page - Set sg entry to point at given page
74  * @sg:          SG entry
75  * @page:        The page
76  * @len:         Length of data
77  * @offset:      Offset into page
78  *
79  * Description:
80  *   Use this function to set an sg entry pointing at a page, never assign
81  *   the page directly. We encode sg table information in the lower bits
82  *   of the page pointer. See sg_page() for looking up the page belonging
83  *   to an sg entry.
84  *
85  **/
86 static inline void sg_set_page(struct scatterlist *sg, struct page *page,
87                                unsigned int len, unsigned int offset)
88 {
89         sg_assign_page(sg, page);
90         sg->offset = offset;
91         sg->length = len;
92 }
93
94 static inline struct page *sg_page(struct scatterlist *sg)
95 {
96 #ifdef CONFIG_DEBUG_SG
97         BUG_ON(sg->sg_magic != SG_MAGIC);
98         BUG_ON(sg_is_chain(sg));
99 #endif
100         return (struct page *)((sg)->page_link & ~0x3);
101 }
102
103 /**
104  * sg_set_buf - Set sg entry to point at given data
105  * @sg:          SG entry
106  * @buf:         Data
107  * @buflen:      Data length
108  *
109  **/
110 static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
111                               unsigned int buflen)
112 {
113         sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
114 }
115
116 /*
117  * Loop over each sg element, following the pointer to a new list if necessary
118  */
119 #define for_each_sg(sglist, sg, nr, __i)        \
120         for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
121
122 /**
123  * sg_chain - Chain two sglists together
124  * @prv:        First scatterlist
125  * @prv_nents:  Number of entries in prv
126  * @sgl:        Second scatterlist
127  *
128  * Description:
129  *   Links @prv@ and @sgl@ together, to form a longer scatterlist.
130  *
131  **/
132 static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents,
133                             struct scatterlist *sgl)
134 {
135 #ifndef ARCH_HAS_SG_CHAIN
136         BUG();
137 #endif
138
139         /*
140          * offset and length are unused for chain entry.  Clear them.
141          */
142         prv[prv_nents - 1].offset = 0;
143         prv[prv_nents - 1].length = 0;
144
145         /*
146          * Set lowest bit to indicate a link pointer, and make sure to clear
147          * the termination bit if it happens to be set.
148          */
149         prv[prv_nents - 1].page_link = ((unsigned long) sgl | 0x01) & ~0x02;
150 }
151
152 /**
153  * sg_mark_end - Mark the end of the scatterlist
154  * @sg:          SG entryScatterlist
155  *
156  * Description:
157  *   Marks the passed in sg entry as the termination point for the sg
158  *   table. A call to sg_next() on this entry will return NULL.
159  *
160  **/
161 static inline void sg_mark_end(struct scatterlist *sg)
162 {
163 #ifdef CONFIG_DEBUG_SG
164         BUG_ON(sg->sg_magic != SG_MAGIC);
165 #endif
166         /*
167          * Set termination bit, clear potential chain bit
168          */
169         sg->page_link |= 0x02;
170         sg->page_link &= ~0x01;
171 }
172
173 /**
174  * sg_phys - Return physical address of an sg entry
175  * @sg:      SG entry
176  *
177  * Description:
178  *   This calls page_to_phys() on the page in this sg entry, and adds the
179  *   sg offset. The caller must know that it is legal to call page_to_phys()
180  *   on the sg page.
181  *
182  **/
183 static inline dma_addr_t sg_phys(struct scatterlist *sg)
184 {
185         return page_to_phys(sg_page(sg)) + sg->offset;
186 }
187
188 /**
189  * sg_virt - Return virtual address of an sg entry
190  * @sg:      SG entry
191  *
192  * Description:
193  *   This calls page_address() on the page in this sg entry, and adds the
194  *   sg offset. The caller must know that the sg page has a valid virtual
195  *   mapping.
196  *
197  **/
198 static inline void *sg_virt(struct scatterlist *sg)
199 {
200         return page_address(sg_page(sg)) + sg->offset;
201 }
202
203 struct scatterlist *sg_next(struct scatterlist *);
204 struct scatterlist *sg_last(struct scatterlist *s, unsigned int);
205 void sg_init_table(struct scatterlist *, unsigned int);
206 void sg_init_one(struct scatterlist *, const void *, unsigned int);
207
208 typedef struct scatterlist *(sg_alloc_fn)(unsigned int, gfp_t);
209 typedef void (sg_free_fn)(struct scatterlist *, unsigned int);
210
211 void __sg_free_table(struct sg_table *, unsigned int, sg_free_fn *);
212 void sg_free_table(struct sg_table *);
213 int __sg_alloc_table(struct sg_table *, unsigned int, unsigned int, gfp_t,
214                      sg_alloc_fn *);
215 int sg_alloc_table(struct sg_table *, unsigned int, gfp_t);
216
217 size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
218                            void *buf, size_t buflen);
219 size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
220                          void *buf, size_t buflen);
221
222 int sg_copy(struct scatterlist *dst_sg, struct scatterlist *src_sg,
223             int nents_to_copy, size_t copy_len,
224             enum km_type d_km_type, enum km_type s_km_type);
225
226 /*
227  * Maximum number of entries that will be allocated in one piece, if
228  * a list larger than this is required then chaining will be utilized.
229  */
230 #define SG_MAX_SINGLE_ALLOC             (PAGE_SIZE / sizeof(struct scatterlist))
231
232
233 /*
234  * Mapping sg iterator
235  *
236  * Iterates over sg entries mapping page-by-page.  On each successful
237  * iteration, @miter->page points to the mapped page and
238  * @miter->length bytes of data can be accessed at @miter->addr.  As
239  * long as an interation is enclosed between start and stop, the user
240  * is free to choose control structure and when to stop.
241  *
242  * @miter->consumed is set to @miter->length on each iteration.  It
243  * can be adjusted if the user can't consume all the bytes in one go.
244  * Also, a stopped iteration can be resumed by calling next on it.
245  * This is useful when iteration needs to release all resources and
246  * continue later (e.g. at the next interrupt).
247  */
248
249 #define SG_MITER_ATOMIC         (1 << 0)         /* use kmap_atomic */
250 #define SG_MITER_TO_SG          (1 << 1)        /* flush back to phys on unmap */
251 #define SG_MITER_FROM_SG        (1 << 2)        /* nop */
252
253 struct sg_mapping_iter {
254         /* the following three fields can be accessed directly */
255         struct page             *page;          /* currently mapped page */
256         void                    *addr;          /* pointer to the mapped area */
257         size_t                  length;         /* length of the mapped area */
258         size_t                  consumed;       /* number of consumed bytes */
259
260         /* these are internal states, keep away */
261         struct scatterlist      *__sg;          /* current entry */
262         unsigned int            __nents;        /* nr of remaining entries */
263         unsigned int            __offset;       /* offset within sg */
264         unsigned int            __flags;
265 };
266
267 void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
268                     unsigned int nents, unsigned int flags);
269 bool sg_miter_next(struct sg_mapping_iter *miter);
270 void sg_miter_stop(struct sg_mapping_iter *miter);
271
272 #endif /* _LINUX_SCATTERLIST_H */