gro: Allow tunnel stacking in the case of FOU/GUE
[linux/fpc-iii.git] / crypto / scatterwalk.c
blob46dcbbc4f32ea3525712dd06d33e71697c4a6c99
1 /*
2 * Cryptographic API.
4 * Cipher operations.
6 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
7 * 2002 Adam J. Richter <adam@yggdrasil.com>
8 * 2004 Jean-Luc Cooke <jlcooke@certainkey.com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
17 #include <crypto/scatterwalk.h>
18 #include <linux/kernel.h>
19 #include <linux/mm.h>
20 #include <linux/module.h>
21 #include <linux/pagemap.h>
22 #include <linux/highmem.h>
23 #include <linux/scatterlist.h>
25 static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
27 void *src = out ? buf : sgdata;
28 void *dst = out ? sgdata : buf;
30 memcpy(dst, src, nbytes);
33 void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg)
35 walk->sg = sg;
37 BUG_ON(!sg->length);
39 walk->offset = sg->offset;
41 EXPORT_SYMBOL_GPL(scatterwalk_start);
43 void *scatterwalk_map(struct scatter_walk *walk)
45 return kmap_atomic(scatterwalk_page(walk)) +
46 offset_in_page(walk->offset);
48 EXPORT_SYMBOL_GPL(scatterwalk_map);
50 static void scatterwalk_pagedone(struct scatter_walk *walk, int out,
51 unsigned int more)
53 if (out) {
54 struct page *page;
56 page = sg_page(walk->sg) + ((walk->offset - 1) >> PAGE_SHIFT);
57 if (!PageSlab(page))
58 flush_dcache_page(page);
61 if (more) {
62 walk->offset += PAGE_SIZE - 1;
63 walk->offset &= PAGE_MASK;
64 if (walk->offset >= walk->sg->offset + walk->sg->length)
65 scatterwalk_start(walk, sg_next(walk->sg));
69 void scatterwalk_done(struct scatter_walk *walk, int out, int more)
71 if (!more || walk->offset >= walk->sg->offset + walk->sg->length ||
72 !(walk->offset & (PAGE_SIZE - 1)))
73 scatterwalk_pagedone(walk, out, more);
75 EXPORT_SYMBOL_GPL(scatterwalk_done);
77 void scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
78 size_t nbytes, int out)
80 for (;;) {
81 unsigned int len_this_page = scatterwalk_pagelen(walk);
82 u8 *vaddr;
84 if (len_this_page > nbytes)
85 len_this_page = nbytes;
87 vaddr = scatterwalk_map(walk);
88 memcpy_dir(buf, vaddr, len_this_page, out);
89 scatterwalk_unmap(vaddr);
91 scatterwalk_advance(walk, len_this_page);
93 if (nbytes == len_this_page)
94 break;
96 buf += len_this_page;
97 nbytes -= len_this_page;
99 scatterwalk_pagedone(walk, out, 1);
102 EXPORT_SYMBOL_GPL(scatterwalk_copychunks);
104 void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg,
105 unsigned int start, unsigned int nbytes, int out)
107 struct scatter_walk walk;
108 unsigned int offset = 0;
110 if (!nbytes)
111 return;
113 for (;;) {
114 scatterwalk_start(&walk, sg);
116 if (start < offset + sg->length)
117 break;
119 offset += sg->length;
120 sg = sg_next(sg);
123 scatterwalk_advance(&walk, start - offset);
124 scatterwalk_copychunks(buf, &walk, nbytes, out);
125 scatterwalk_done(&walk, out, 0);
127 EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy);
129 int scatterwalk_bytes_sglen(struct scatterlist *sg, int num_bytes)
131 int offset = 0, n = 0;
133 /* num_bytes is too small */
134 if (num_bytes < sg->length)
135 return -1;
137 do {
138 offset += sg->length;
139 n++;
140 sg = sg_next(sg);
142 /* num_bytes is too large */
143 if (unlikely(!sg && (num_bytes < offset)))
144 return -1;
145 } while (sg && (num_bytes > offset));
147 return n;
149 EXPORT_SYMBOL_GPL(scatterwalk_bytes_sglen);