1
2
3
4
5
6
7
8
9#include <linux/module.h>
10#include <linux/scatterlist.h>
11#include <linux/highmem.h>
12
13
14
15
16
17
18
19
20
21
22
23struct scatterlist *sg_next(struct scatterlist *sg)
24{
25#ifdef CONFIG_DEBUG_SG
26 BUG_ON(sg->sg_magic != SG_MAGIC);
27#endif
28 if (sg_is_last(sg))
29 return NULL;
30
31 sg++;
32 if (unlikely(sg_is_chain(sg)))
33 sg = sg_chain_ptr(sg);
34
35 return sg;
36}
37EXPORT_SYMBOL(sg_next);
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53struct scatterlist *sg_last(struct scatterlist *sgl, unsigned int nents)
54{
55#ifndef ARCH_HAS_SG_CHAIN
56 struct scatterlist *ret = &sgl[nents - 1];
57#else
58 struct scatterlist *sg, *ret = NULL;
59 unsigned int i;
60
61 for_each_sg(sgl, sg, nents, i)
62 ret = sg;
63
64#endif
65#ifdef CONFIG_DEBUG_SG
66 BUG_ON(sgl[0].sg_magic != SG_MAGIC);
67 BUG_ON(!sg_is_last(ret));
68#endif
69 return ret;
70}
71EXPORT_SYMBOL(sg_last);
72
73
74
75
76
77
78
79
80
81
82
83void sg_init_table(struct scatterlist *sgl, unsigned int nents)
84{
85 memset(sgl, 0, sizeof(*sgl) * nents);
86#ifdef CONFIG_DEBUG_SG
87 {
88 unsigned int i;
89 for (i = 0; i < nents; i++)
90 sgl[i].sg_magic = SG_MAGIC;
91 }
92#endif
93 sg_mark_end(&sgl[nents - 1]);
94}
95EXPORT_SYMBOL(sg_init_table);
96
97
98
99
100
101
102
103
104void sg_init_one(struct scatterlist *sg, const void *buf, unsigned int buflen)
105{
106 sg_init_table(sg, 1);
107 sg_set_buf(sg, buf, buflen);
108}
109EXPORT_SYMBOL(sg_init_one);
110
111
112
113
114
115static struct scatterlist *sg_kmalloc(unsigned int nents, gfp_t gfp_mask)
116{
117 if (nents == SG_MAX_SINGLE_ALLOC)
118 return (struct scatterlist *) __get_free_page(gfp_mask);
119 else
120 return kmalloc(nents * sizeof(struct scatterlist), gfp_mask);
121}
122
123static void sg_kfree(struct scatterlist *sg, unsigned int nents)
124{
125 if (nents == SG_MAX_SINGLE_ALLOC)
126 free_page((unsigned long) sg);
127 else
128 kfree(sg);
129}
130
131
132
133
134
135
136
137
138
139
140
141
142
143void __sg_free_table(struct sg_table *table, unsigned int max_ents,
144 sg_free_fn *free_fn)
145{
146 struct scatterlist *sgl, *next;
147
148 if (unlikely(!table->sgl))
149 return;
150
151 sgl = table->sgl;
152 while (table->orig_nents) {
153 unsigned int alloc_size = table->orig_nents;
154 unsigned int sg_size;
155
156
157
158
159
160
161
162 if (alloc_size > max_ents) {
163 next = sg_chain_ptr(&sgl[max_ents - 1]);
164 alloc_size = max_ents;
165 sg_size = alloc_size - 1;
166 } else {
167 sg_size = alloc_size;
168 next = NULL;
169 }
170
171 table->orig_nents -= sg_size;
172 free_fn(sgl, alloc_size);
173 sgl = next;
174 }
175
176 table->sgl = NULL;
177}
178EXPORT_SYMBOL(__sg_free_table);
179
180
181
182
183
184
185void sg_free_table(struct sg_table *table)
186{
187 __sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree);
188}
189EXPORT_SYMBOL(sg_free_table);
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210int __sg_alloc_table(struct sg_table *table, unsigned int nents,
211 unsigned int max_ents, gfp_t gfp_mask,
212 sg_alloc_fn *alloc_fn)
213{
214 struct scatterlist *sg, *prv;
215 unsigned int left;
216
217#ifndef ARCH_HAS_SG_CHAIN
218 BUG_ON(nents > max_ents);
219#endif
220
221 memset(table, 0, sizeof(*table));
222
223 left = nents;
224 prv = NULL;
225 do {
226 unsigned int sg_size, alloc_size = left;
227
228 if (alloc_size > max_ents) {
229 alloc_size = max_ents;
230 sg_size = alloc_size - 1;
231 } else
232 sg_size = alloc_size;
233
234 left -= sg_size;
235
236 sg = alloc_fn(alloc_size, gfp_mask);
237 if (unlikely(!sg))
238 return -ENOMEM;
239
240 sg_init_table(sg, alloc_size);
241 table->nents = table->orig_nents += sg_size;
242
243
244
245
246
247 if (prv)
248 sg_chain(prv, max_ents, sg);
249 else
250 table->sgl = sg;
251
252
253
254
255 if (!left)
256 sg_mark_end(&sg[sg_size - 1]);
257
258
259
260
261
262
263
264 gfp_mask &= ~__GFP_WAIT;
265 gfp_mask |= __GFP_HIGH;
266 prv = sg;
267 } while (left);
268
269 return 0;
270}
271EXPORT_SYMBOL(__sg_alloc_table);
272
273
274
275
276
277
278
279
280
281
282
283
284int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
285{
286 int ret;
287
288 ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
289 gfp_mask, sg_kmalloc);
290 if (unlikely(ret))
291 __sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree);
292
293 return ret;
294}
295EXPORT_SYMBOL(sg_alloc_table);
296
297
298
299
300
301
302
303
304
305
306
307
308
309void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
310 unsigned int nents, unsigned int flags)
311{
312 memset(miter, 0, sizeof(struct sg_mapping_iter));
313
314 miter->__sg = sgl;
315 miter->__nents = nents;
316 miter->__offset = 0;
317 miter->__flags = flags;
318}
319EXPORT_SYMBOL(sg_miter_start);
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339bool sg_miter_next(struct sg_mapping_iter *miter)
340{
341 unsigned int off, len;
342
343
344 if (!miter->__nents)
345 return false;
346
347 sg_miter_stop(miter);
348
349
350 while (miter->__offset == miter->__sg->length) {
351 if (--miter->__nents) {
352 miter->__sg = sg_next(miter->__sg);
353 miter->__offset = 0;
354 } else
355 return false;
356 }
357
358
359 off = miter->__sg->offset + miter->__offset;
360 len = miter->__sg->length - miter->__offset;
361
362 miter->page = nth_page(sg_page(miter->__sg), off >> PAGE_SHIFT);
363 off &= ~PAGE_MASK;
364 miter->length = min_t(unsigned int, len, PAGE_SIZE - off);
365 miter->consumed = miter->length;
366
367 if (miter->__flags & SG_MITER_ATOMIC)
368 miter->addr = kmap_atomic(miter->page, KM_BIO_SRC_IRQ) + off;
369 else
370 miter->addr = kmap(miter->page) + off;
371
372 return true;
373}
374EXPORT_SYMBOL(sg_miter_next);
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389void sg_miter_stop(struct sg_mapping_iter *miter)
390{
391 WARN_ON(miter->consumed > miter->length);
392
393
394 if (miter->addr) {
395 miter->__offset += miter->consumed;
396
397 if (miter->__flags & SG_MITER_ATOMIC) {
398 WARN_ON(!irqs_disabled());
399 kunmap_atomic(miter->addr, KM_BIO_SRC_IRQ);
400 } else
401 kunmap(miter->page);
402
403 miter->page = NULL;
404 miter->addr = NULL;
405 miter->length = 0;
406 miter->consumed = 0;
407 }
408}
409EXPORT_SYMBOL(sg_miter_stop);
410
411
412
413
414
415
416
417
418
419
420
421
422
423static size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents,
424 void *buf, size_t buflen, int to_buffer)
425{
426 unsigned int offset = 0;
427 struct sg_mapping_iter miter;
428 unsigned long flags;
429
430 sg_miter_start(&miter, sgl, nents, SG_MITER_ATOMIC);
431
432 local_irq_save(flags);
433
434 while (sg_miter_next(&miter) && offset < buflen) {
435 unsigned int len;
436
437 len = min(miter.length, buflen - offset);
438
439 if (to_buffer)
440 memcpy(buf + offset, miter.addr, len);
441 else {
442 memcpy(miter.addr, buf + offset, len);
443 flush_kernel_dcache_page(miter.page);
444 }
445
446 offset += len;
447 }
448
449 sg_miter_stop(&miter);
450
451 local_irq_restore(flags);
452 return offset;
453}
454
455
456
457
458
459
460
461
462
463
464
465size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
466 void *buf, size_t buflen)
467{
468 return sg_copy_buffer(sgl, nents, buf, buflen, 0);
469}
470EXPORT_SYMBOL(sg_copy_from_buffer);
471
472
473
474
475
476
477
478
479
480
481
482size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
483 void *buf, size_t buflen)
484{
485 return sg_copy_buffer(sgl, nents, buf, buflen, 1);
486}
487EXPORT_SYMBOL(sg_copy_to_buffer);
488