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)
16 #include <linux/kernel.h>
18 #include <linux/pagemap.h>
19 #include <linux/highmem.h>
20 #include <asm/scatterlist.h>
22 #include "scatterwalk.h"
24 enum km_type crypto_km_types[] = {
31 void *scatterwalk_whichbuf(struct scatter_walk *walk, unsigned int nbytes, void *scratch)
33 if (nbytes <= walk->len_this_page &&
34 (((unsigned long)walk->data) & (PAGE_CACHE_SIZE - 1)) + nbytes <=
41 static void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
44 memcpy(sgdata, buf, nbytes);
46 memcpy(buf, sgdata, nbytes);
49 void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg)
51 unsigned int rest_of_page;
55 walk->page = sg->page;
56 walk->len_this_segment = sg->length;
58 rest_of_page = PAGE_CACHE_SIZE - (sg->offset & (PAGE_CACHE_SIZE - 1));
59 walk->len_this_page = min(sg->length, rest_of_page);
60 walk->offset = sg->offset;
63 void scatterwalk_map(struct scatter_walk *walk, int out)
65 walk->data = crypto_kmap(walk->page, out) + walk->offset;
68 static void scatterwalk_pagedone(struct scatter_walk *walk, int out,
71 /* walk->data may be pointing the first byte of the next page;
72 however, we know we transfered at least one byte. So,
73 walk->data - 1 will be a virutual address in the mapped page. */
76 flush_dcache_page(walk->page);
79 walk->len_this_segment -= walk->len_this_page;
81 if (walk->len_this_segment) {
83 walk->len_this_page = min(walk->len_this_segment,
84 (unsigned)PAGE_CACHE_SIZE);
88 scatterwalk_start(walk, sg_next(walk->sg));
92 void scatterwalk_done(struct scatter_walk *walk, int out, int more)
94 crypto_kunmap(walk->data, out);
95 if (walk->len_this_page == 0 || !more)
96 scatterwalk_pagedone(walk, out, more);
100 * Do not call this unless the total length of all of the fragments
101 * has been verified as multiple of the block size.
103 int scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
104 size_t nbytes, int out)
106 if (buf != walk->data) {
107 while (nbytes > walk->len_this_page) {
108 memcpy_dir(buf, walk->data, walk->len_this_page, out);
109 buf += walk->len_this_page;
110 nbytes -= walk->len_this_page;
112 crypto_kunmap(walk->data, out);
113 scatterwalk_pagedone(walk, out, 1);
114 scatterwalk_map(walk, out);
117 memcpy_dir(buf, walk->data, nbytes, out);
120 walk->offset += nbytes;
121 walk->len_this_page -= nbytes;
122 walk->len_this_segment -= nbytes;