Merge branch 'i2c-for-linus' of git://jdelvare.pck.nerim.net/jdelvare-2.6
[pv_ops_mirror.git] / crypto / scatterwalk.c
blobb9bbda0bb9f968efc4a728dd316952b7516f617b
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.
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>
23 #include "internal.h"
24 #include "scatterwalk.h"
26 static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
28 void *src = out ? buf : sgdata;
29 void *dst = out ? sgdata : buf;
31 memcpy(dst, src, nbytes);
34 void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg)
36 walk->sg = sg;
38 BUG_ON(!sg->length);
40 walk->offset = sg->offset;
42 EXPORT_SYMBOL_GPL(scatterwalk_start);
44 void *scatterwalk_map(struct scatter_walk *walk, int out)
46 return crypto_kmap(scatterwalk_page(walk), out) +
47 offset_in_page(walk->offset);
49 EXPORT_SYMBOL_GPL(scatterwalk_map);
51 static void scatterwalk_pagedone(struct scatter_walk *walk, int out,
52 unsigned int more)
54 if (out) {
55 struct page *page;
57 page = sg_page(walk->sg) + ((walk->offset - 1) >> PAGE_SHIFT);
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, scatterwalk_sg_next(walk->sg));
69 void scatterwalk_done(struct scatter_walk *walk, int out, int more)
71 if (!offset_in_page(walk->offset) || !more)
72 scatterwalk_pagedone(walk, out, more);
74 EXPORT_SYMBOL_GPL(scatterwalk_done);
76 void scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
77 size_t nbytes, int out)
79 for (;;) {
80 unsigned int len_this_page = scatterwalk_pagelen(walk);
81 u8 *vaddr;
83 if (len_this_page > nbytes)
84 len_this_page = nbytes;
86 vaddr = scatterwalk_map(walk, out);
87 memcpy_dir(buf, vaddr, len_this_page, out);
88 scatterwalk_unmap(vaddr, out);
90 scatterwalk_advance(walk, len_this_page);
92 if (nbytes == len_this_page)
93 break;
95 buf += len_this_page;
96 nbytes -= len_this_page;
98 scatterwalk_pagedone(walk, out, 1);
101 EXPORT_SYMBOL_GPL(scatterwalk_copychunks);
103 void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg,
104 unsigned int start, unsigned int nbytes, int out)
106 struct scatter_walk walk;
107 unsigned int offset = 0;
109 for (;;) {
110 scatterwalk_start(&walk, sg);
112 if (start < offset + sg->length)
113 break;
115 offset += sg->length;
116 sg = sg_next(sg);
119 scatterwalk_advance(&walk, start - offset);
120 scatterwalk_copychunks(buf, &walk, nbytes, out);
121 scatterwalk_done(&walk, out, 0);
123 EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy);