]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - crypto/scatterwalk.c
i2c-nomadik: Fixup clock handling
[mirror_ubuntu-artful-kernel.git] / crypto / scatterwalk.c
CommitLineData
1da177e4
LT
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 */
42c271c6
HX
16
17#include <crypto/scatterwalk.h>
1da177e4
LT
18#include <linux/kernel.h>
19#include <linux/mm.h>
5c64097a 20#include <linux/module.h>
1da177e4
LT
21#include <linux/pagemap.h>
22#include <linux/highmem.h>
5c64097a
HX
23#include <linux/scatterlist.h>
24
5c64097a 25static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
1da177e4 26{
5c64097a
HX
27 void *src = out ? buf : sgdata;
28 void *dst = out ? sgdata : buf;
29
30 memcpy(dst, src, nbytes);
1da177e4
LT
31}
32
33void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg)
34{
1da177e4
LT
35 walk->sg = sg;
36
1da177e4
LT
37 BUG_ON(!sg->length);
38
1da177e4
LT
39 walk->offset = sg->offset;
40}
5c64097a 41EXPORT_SYMBOL_GPL(scatterwalk_start);
1da177e4 42
f0dfc0b0 43void *scatterwalk_map(struct scatter_walk *walk)
1da177e4 44{
f0dfc0b0 45 return kmap_atomic(scatterwalk_page(walk)) +
5c64097a 46 offset_in_page(walk->offset);
1da177e4 47}
5c64097a 48EXPORT_SYMBOL_GPL(scatterwalk_map);
1da177e4
LT
49
50static void scatterwalk_pagedone(struct scatter_walk *walk, int out,
51 unsigned int more)
52{
9f116727
HX
53 if (out) {
54 struct page *page;
55
78c2f0b8 56 page = sg_page(walk->sg) + ((walk->offset - 1) >> PAGE_SHIFT);
4f3e797a
HX
57 if (!PageSlab(page))
58 flush_dcache_page(page);
9f116727 59 }
1da177e4
LT
60
61 if (more) {
5c64097a
HX
62 walk->offset += PAGE_SIZE - 1;
63 walk->offset &= PAGE_MASK;
64 if (walk->offset >= walk->sg->offset + walk->sg->length)
b2ab4a57 65 scatterwalk_start(walk, scatterwalk_sg_next(walk->sg));
1da177e4
LT
66 }
67}
68
69void scatterwalk_done(struct scatter_walk *walk, int out, int more)
70{
85c6201a 71 if (!(scatterwalk_pagelen(walk) & (PAGE_SIZE - 1)) || !more)
1da177e4
LT
72 scatterwalk_pagedone(walk, out, more);
73}
5c64097a 74EXPORT_SYMBOL_GPL(scatterwalk_done);
1da177e4 75
5c64097a
HX
76void scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
77 size_t nbytes, int out)
1da177e4 78{
5c64097a
HX
79 for (;;) {
80 unsigned int len_this_page = scatterwalk_pagelen(walk);
81 u8 *vaddr;
82
83 if (len_this_page > nbytes)
84 len_this_page = nbytes;
85
f0dfc0b0 86 vaddr = scatterwalk_map(walk);
5c64097a 87 memcpy_dir(buf, vaddr, len_this_page, out);
f0dfc0b0 88 scatterwalk_unmap(vaddr);
5c64097a 89
4ee531a3 90 scatterwalk_advance(walk, len_this_page);
f70ee5ec 91
5c64097a
HX
92 if (nbytes == len_this_page)
93 break;
94
95 buf += len_this_page;
96 nbytes -= len_this_page;
1da177e4 97
1da177e4 98 scatterwalk_pagedone(walk, out, 1);
c774e93e 99 }
1da177e4 100}
5c64097a 101EXPORT_SYMBOL_GPL(scatterwalk_copychunks);
5fa0fea2
HX
102
103void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg,
104 unsigned int start, unsigned int nbytes, int out)
105{
106 struct scatter_walk walk;
107 unsigned int offset = 0;
108
6e050778
HX
109 if (!nbytes)
110 return;
111
5fa0fea2
HX
112 for (;;) {
113 scatterwalk_start(&walk, sg);
114
115 if (start < offset + sg->length)
116 break;
117
118 offset += sg->length;
b2ab4a57 119 sg = scatterwalk_sg_next(sg);
5fa0fea2
HX
120 }
121
122 scatterwalk_advance(&walk, start - offset);
123 scatterwalk_copychunks(buf, &walk, nbytes, out);
124 scatterwalk_done(&walk, out, 0);
125}
126EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy);