Merge branch 'upstream-linus' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[linux-2.6] / 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 #include <linux/kernel.h>
17 #include <linux/mm.h>
18 #include <linux/module.h>
19 #include <linux/pagemap.h>
20 #include <linux/highmem.h>
21 #include <linux/scatterlist.h>
22
23 #include "internal.h"
24 #include "scatterwalk.h"
25
26 enum km_type crypto_km_types[] = {
27         KM_USER0,
28         KM_USER1,
29         KM_SOFTIRQ0,
30         KM_SOFTIRQ1,
31 };
32 EXPORT_SYMBOL_GPL(crypto_km_types);
33
34 static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
35 {
36         void *src = out ? buf : sgdata;
37         void *dst = out ? sgdata : buf;
38
39         memcpy(dst, src, nbytes);
40 }
41
42 void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg)
43 {
44         walk->sg = sg;
45
46         BUG_ON(!sg->length);
47
48         walk->offset = sg->offset;
49 }
50 EXPORT_SYMBOL_GPL(scatterwalk_start);
51
52 void *scatterwalk_map(struct scatter_walk *walk, int out)
53 {
54         return crypto_kmap(scatterwalk_page(walk), out) +
55                offset_in_page(walk->offset);
56 }
57 EXPORT_SYMBOL_GPL(scatterwalk_map);
58
59 static void scatterwalk_pagedone(struct scatter_walk *walk, int out,
60                                  unsigned int more)
61 {
62         if (out) {
63                 struct page *page;
64
65                 page = walk->sg->page + ((walk->offset - 1) >> PAGE_SHIFT);
66                 flush_dcache_page(page);
67         }
68
69         if (more) {
70                 walk->offset += PAGE_SIZE - 1;
71                 walk->offset &= PAGE_MASK;
72                 if (walk->offset >= walk->sg->offset + walk->sg->length)
73                         scatterwalk_start(walk, sg_next(walk->sg));
74         }
75 }
76
77 void scatterwalk_done(struct scatter_walk *walk, int out, int more)
78 {
79         if (!offset_in_page(walk->offset) || !more)
80                 scatterwalk_pagedone(walk, out, more);
81 }
82 EXPORT_SYMBOL_GPL(scatterwalk_done);
83
84 void scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
85                             size_t nbytes, int out)
86 {
87         for (;;) {
88                 unsigned int len_this_page = scatterwalk_pagelen(walk);
89                 u8 *vaddr;
90
91                 if (len_this_page > nbytes)
92                         len_this_page = nbytes;
93
94                 vaddr = scatterwalk_map(walk, out);
95                 memcpy_dir(buf, vaddr, len_this_page, out);
96                 scatterwalk_unmap(vaddr, out);
97
98                 scatterwalk_advance(walk, len_this_page);
99
100                 if (nbytes == len_this_page)
101                         break;
102
103                 buf += len_this_page;
104                 nbytes -= len_this_page;
105
106                 scatterwalk_pagedone(walk, out, 1);
107         }
108 }
109 EXPORT_SYMBOL_GPL(scatterwalk_copychunks);