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)
17
#include <linux/version.h>
18
#if(LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
19
#include "kmap_types.h"
22
#include <linux/kernel.h>
24
#include <linux/pagemap.h>
25
#include <linux/highmem.h>
26
#if(LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24))
27
#include <asm/scatterlist.h>
29
#include <linux/scatterlist.h>
32
#include "scatterwalk.h"
34
enum km_type crypto_km_types[] = {
41
void *scatterwalk_whichbuf(struct scatter_walk *walk, unsigned int nbytes, void *scratch)
43
if (nbytes <= walk->len_this_page &&
44
(((unsigned long)walk->data) & (PAGE_CACHE_SIZE - 1)) + nbytes <=
51
static void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
54
memcpy(sgdata, buf, nbytes);
56
memcpy(buf, sgdata, nbytes);
59
void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg)
61
unsigned int rest_of_page;
65
#if(LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24))
66
walk->page = sg->page;
68
walk->page = sg_page(sg);
70
walk->len_this_segment = sg->length;
72
rest_of_page = PAGE_CACHE_SIZE - (sg->offset & (PAGE_CACHE_SIZE - 1));
73
walk->len_this_page = min(sg->length, rest_of_page);
74
walk->offset = sg->offset;
77
void scatterwalk_map(struct scatter_walk *walk, int out)
79
walk->data = crypto_kmap(walk->page, out) + walk->offset;
82
static void scatterwalk_pagedone(struct scatter_walk *walk, int out,
85
/* walk->data may be pointing the first byte of the next page;
86
however, we know we transfered at least one byte. So,
87
walk->data - 1 will be a virtual address in the mapped page. */
90
flush_dcache_page(walk->page);
93
walk->len_this_segment -= walk->len_this_page;
95
if (walk->len_this_segment) {
97
walk->len_this_page = min(walk->len_this_segment,
98
(unsigned)PAGE_CACHE_SIZE);
102
scatterwalk_start(walk, sg_next(walk->sg));
106
void scatterwalk_done(struct scatter_walk *walk, int out, int more)
108
crypto_kunmap(walk->data, out);
109
if (walk->len_this_page == 0 || !more)
110
scatterwalk_pagedone(walk, out, more);
114
* Do not call this unless the total length of all of the fragments
115
* has been verified as multiple of the block size.
117
int scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
118
size_t nbytes, int out)
120
if (buf != walk->data) {
121
while (nbytes > walk->len_this_page) {
122
memcpy_dir(buf, walk->data, walk->len_this_page, out);
123
buf += walk->len_this_page;
124
nbytes -= walk->len_this_page;
126
crypto_kunmap(walk->data, out);
127
scatterwalk_pagedone(walk, out, 1);
128
scatterwalk_map(walk, out);
131
memcpy_dir(buf, walk->data, nbytes, out);
134
walk->offset += nbytes;
135
walk->len_this_page -= nbytes;
136
walk->len_this_segment -= nbytes;