crypto: scatterwalk - Add no-copy support to copychunks
[cascardo/linux.git] / crypto / scatterwalk.c
1 /*
2  * Cryptographic API.
3  *
4  * Cipher operations.
5  *
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>
9  *
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.
14  *
15  */
16
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>
24
25 static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
26 {
27         void *src = out ? buf : sgdata;
28         void *dst = out ? sgdata : buf;
29
30         memcpy(dst, src, nbytes);
31 }
32
33 void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg)
34 {
35         walk->sg = sg;
36
37         BUG_ON(!sg->length);
38
39         walk->offset = sg->offset;
40 }
41 EXPORT_SYMBOL_GPL(scatterwalk_start);
42
43 void *scatterwalk_map(struct scatter_walk *walk)
44 {
45         return kmap_atomic(scatterwalk_page(walk)) +
46                offset_in_page(walk->offset);
47 }
48 EXPORT_SYMBOL_GPL(scatterwalk_map);
49
50 static void scatterwalk_pagedone(struct scatter_walk *walk, int out,
51                                  unsigned int more)
52 {
53         if (out) {
54                 struct page *page;
55
56                 page = sg_page(walk->sg) + ((walk->offset - 1) >> PAGE_SHIFT);
57                 /* Test ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE first as
58                  * PageSlab cannot be optimised away per se due to
59                  * use of volatile pointer.
60                  */
61                 if (ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE && !PageSlab(page))
62                         flush_dcache_page(page);
63         }
64
65         if (more) {
66                 walk->offset += PAGE_SIZE - 1;
67                 walk->offset &= PAGE_MASK;
68                 if (walk->offset >= walk->sg->offset + walk->sg->length)
69                         scatterwalk_start(walk, sg_next(walk->sg));
70         }
71 }
72
73 void scatterwalk_done(struct scatter_walk *walk, int out, int more)
74 {
75         if (!(scatterwalk_pagelen(walk) & (PAGE_SIZE - 1)) || !more)
76                 scatterwalk_pagedone(walk, out, more);
77 }
78 EXPORT_SYMBOL_GPL(scatterwalk_done);
79
80 void scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
81                             size_t nbytes, int out)
82 {
83         for (;;) {
84                 unsigned int len_this_page = scatterwalk_pagelen(walk);
85                 u8 *vaddr;
86
87                 if (len_this_page > nbytes)
88                         len_this_page = nbytes;
89
90                 if (out != 2) {
91                         vaddr = scatterwalk_map(walk);
92                         memcpy_dir(buf, vaddr, len_this_page, out);
93                         scatterwalk_unmap(vaddr);
94                 }
95
96                 scatterwalk_advance(walk, len_this_page);
97
98                 if (nbytes == len_this_page)
99                         break;
100
101                 buf += len_this_page;
102                 nbytes -= len_this_page;
103
104                 scatterwalk_pagedone(walk, out & 1, 1);
105         }
106 }
107 EXPORT_SYMBOL_GPL(scatterwalk_copychunks);
108
109 void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg,
110                               unsigned int start, unsigned int nbytes, int out)
111 {
112         struct scatter_walk walk;
113         struct scatterlist tmp[2];
114
115         if (!nbytes)
116                 return;
117
118         sg = scatterwalk_ffwd(tmp, sg, start);
119
120         if (sg_page(sg) == virt_to_page(buf) &&
121             sg->offset == offset_in_page(buf))
122                 return;
123
124         scatterwalk_start(&walk, sg);
125         scatterwalk_copychunks(buf, &walk, nbytes, out);
126         scatterwalk_done(&walk, out, 0);
127 }
128 EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy);
129
130 struct scatterlist *scatterwalk_ffwd(struct scatterlist dst[2],
131                                      struct scatterlist *src,
132                                      unsigned int len)
133 {
134         for (;;) {
135                 if (!len)
136                         return src;
137
138                 if (src->length > len)
139                         break;
140
141                 len -= src->length;
142                 src = sg_next(src);
143         }
144
145         sg_init_table(dst, 2);
146         sg_set_page(dst, sg_page(src), src->length - len, src->offset + len);
147         scatterwalk_crypto_chain(dst, sg_next(src), 0, 2);
148
149         return dst;
150 }
151 EXPORT_SYMBOL_GPL(scatterwalk_ffwd);