diff options
author | Jens Axboe <jens.axboe@oracle.com> | 2007-11-30 03:16:50 -0500 |
---|---|---|
committer | Jens Axboe <jens.axboe@oracle.com> | 2008-01-28 04:05:27 -0500 |
commit | 0db9299f48ebd4a860d6ad4e1d36ac50671d48e7 (patch) | |
tree | 37ae5e75f26969b53548a1ea7c69284e6b269bc9 /include | |
parent | 91525300baf162e83e923b09ca286f9205e21522 (diff) |
SG: Move functions to lib/scatterlist.c and add sg chaining allocator helpers
Manually doing chained sg lists is not trivial, so add some helpers
to make sure that drivers get it right.
Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
Diffstat (limited to 'include')
-rw-r--r-- | include/linux/scatterlist.h | 125 |
1 files changed, 25 insertions, 100 deletions
diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h index e3ff21dbac53..e9cb103417b2 100644 --- a/include/linux/scatterlist.h +++ b/include/linux/scatterlist.h | |||
@@ -7,6 +7,12 @@ | |||
7 | #include <linux/string.h> | 7 | #include <linux/string.h> |
8 | #include <asm/io.h> | 8 | #include <asm/io.h> |
9 | 9 | ||
10 | struct sg_table { | ||
11 | struct scatterlist *sgl; /* the list */ | ||
12 | unsigned int nents; /* number of mapped entries */ | ||
13 | unsigned int orig_nents; /* original size of list */ | ||
14 | }; | ||
15 | |||
10 | /* | 16 | /* |
11 | * Notes on SG table design. | 17 | * Notes on SG table design. |
12 | * | 18 | * |
@@ -106,31 +112,6 @@ 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)); | 112 | sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf)); |
107 | } | 113 | } |
108 | 114 | ||
109 | /** | ||
110 | * sg_next - return the next scatterlist entry in a list | ||
111 | * @sg: The current sg entry | ||
112 | * | ||
113 | * Description: | ||
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 | ||
116 | * scatterlist array. | ||
117 | * | ||
118 | **/ | ||
119 | static inline struct scatterlist *sg_next(struct scatterlist *sg) | ||
120 | { | ||
121 | #ifdef CONFIG_DEBUG_SG | ||
122 | BUG_ON(sg->sg_magic != SG_MAGIC); | ||
123 | #endif | ||
124 | if (sg_is_last(sg)) | ||
125 | return NULL; | ||
126 | |||
127 | sg++; | ||
128 | if (unlikely(sg_is_chain(sg))) | ||
129 | sg = sg_chain_ptr(sg); | ||
130 | |||
131 | return sg; | ||
132 | } | ||
133 | |||
134 | /* | 115 | /* |
135 | * Loop over each sg element, following the pointer to a new list if necessary | 116 | * Loop over each sg element, following the pointer to a new list if necessary |
136 | */ | 117 | */ |
@@ -138,40 +119,6 @@ static inline struct scatterlist *sg_next(struct scatterlist *sg) | |||
138 | for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg)) | 119 | for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg)) |
139 | 120 | ||
140 | /** | 121 | /** |
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 | ||
144 | * | ||
145 | * Description: | ||
146 | * Should only be used casually, it (currently) scan the entire list | ||
147 | * to get the last entry. | ||
148 | * | ||
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 | ||
151 | * exist from @sgl@. | ||
152 | * | ||
153 | **/ | ||
154 | static inline struct scatterlist *sg_last(struct scatterlist *sgl, | ||
155 | unsigned int nents) | ||
156 | { | ||
157 | #ifndef ARCH_HAS_SG_CHAIN | ||
158 | struct scatterlist *ret = &sgl[nents - 1]; | ||
159 | #else | ||
160 | struct scatterlist *sg, *ret = NULL; | ||
161 | unsigned int i; | ||
162 | |||
163 | for_each_sg(sgl, sg, nents, i) | ||
164 | ret = sg; | ||
165 | |||
166 | #endif | ||
167 | #ifdef CONFIG_DEBUG_SG | ||
168 | BUG_ON(sgl[0].sg_magic != SG_MAGIC); | ||
169 | BUG_ON(!sg_is_last(ret)); | ||
170 | #endif | ||
171 | return ret; | ||
172 | } | ||
173 | |||
174 | /** | ||
175 | * sg_chain - Chain two sglists together | 122 | * sg_chain - Chain two sglists together |
176 | * @prv: First scatterlist | 123 | * @prv: First scatterlist |
177 | * @prv_nents: Number of entries in prv | 124 | * @prv_nents: Number of entries in prv |
@@ -223,47 +170,6 @@ static inline void sg_mark_end(struct scatterlist *sg) | |||
223 | } | 170 | } |
224 | 171 | ||
225 | /** | 172 | /** |
226 | * sg_init_table - Initialize SG table | ||
227 | * @sgl: The SG table | ||
228 | * @nents: Number of entries in table | ||
229 | * | ||
230 | * Notes: | ||
231 | * If this is part of a chained sg table, sg_mark_end() should be | ||
232 | * used only on the last table part. | ||
233 | * | ||
234 | **/ | ||
235 | static inline void sg_init_table(struct scatterlist *sgl, unsigned int nents) | ||
236 | { | ||
237 | memset(sgl, 0, sizeof(*sgl) * nents); | ||
238 | #ifdef CONFIG_DEBUG_SG | ||
239 | { | ||
240 | unsigned int i; | ||
241 | for (i = 0; i < nents; i++) | ||
242 | sgl[i].sg_magic = SG_MAGIC; | ||
243 | } | ||
244 | #endif | ||
245 | sg_mark_end(&sgl[nents - 1]); | ||
246 | } | ||
247 | |||
248 | /** | ||
249 | * sg_init_one - Initialize a single entry sg list | ||
250 | * @sg: SG entry | ||
251 | * @buf: Virtual address for IO | ||
252 | * @buflen: IO length | ||
253 | * | ||
254 | * Notes: | ||
255 | * This should not be used on a single entry that is part of a larger | ||
256 | * table. Use sg_init_table() for that. | ||
257 | * | ||
258 | **/ | ||
259 | static inline void sg_init_one(struct scatterlist *sg, const void *buf, | ||
260 | unsigned int buflen) | ||
261 | { | ||
262 | sg_init_table(sg, 1); | ||
263 | sg_set_buf(sg, buf, buflen); | ||
264 | } | ||
265 | |||
266 | /** | ||
267 | * sg_phys - Return physical address of an sg entry | 173 | * sg_phys - Return physical address of an sg entry |
268 | * @sg: SG entry | 174 | * @sg: SG entry |
269 | * | 175 | * |
@@ -293,4 +199,23 @@ static inline void *sg_virt(struct scatterlist *sg) | |||
293 | return page_address(sg_page(sg)) + sg->offset; | 199 | return page_address(sg_page(sg)) + sg->offset; |
294 | } | 200 | } |
295 | 201 | ||
202 | struct scatterlist *sg_next(struct scatterlist *); | ||
203 | struct scatterlist *sg_last(struct scatterlist *s, unsigned int); | ||
204 | void sg_init_table(struct scatterlist *, unsigned int); | ||
205 | void sg_init_one(struct scatterlist *, const void *, unsigned int); | ||
206 | |||
207 | typedef struct scatterlist *(sg_alloc_fn)(unsigned int, gfp_t); | ||
208 | typedef void (sg_free_fn)(struct scatterlist *, unsigned int); | ||
209 | |||
210 | void __sg_free_table(struct sg_table *, sg_free_fn *); | ||
211 | void sg_free_table(struct sg_table *); | ||
212 | int __sg_alloc_table(struct sg_table *, unsigned int, gfp_t, sg_alloc_fn *); | ||
213 | int sg_alloc_table(struct sg_table *, unsigned int, gfp_t); | ||
214 | |||
215 | /* | ||
216 | * Maximum number of entries that will be allocated in one piece, if | ||
217 | * a list larger than this is required then chaining will be utilized. | ||
218 | */ | ||
219 | #define SG_MAX_SINGLE_ALLOC (PAGE_SIZE / sizeof(struct scatterlist)) | ||
220 | |||
296 | #endif /* _LINUX_SCATTERLIST_H */ | 221 | #endif /* _LINUX_SCATTERLIST_H */ |