xref: /OK3568_Linux_fs/kernel/drivers/crypto/rockchip/cryptodev_linux/util.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2011 Maxim Levitsky
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * This file is part of linux cryptodev.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or
7*4882a593Smuzhiyun  * modify it under the terms of the GNU General Public License
8*4882a593Smuzhiyun  * as published by the Free Software Foundation; either version 2
9*4882a593Smuzhiyun  * of the License, or (at your option) any later version.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * This program is distributed in the hope that it will be useful,
12*4882a593Smuzhiyun  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*4882a593Smuzhiyun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*4882a593Smuzhiyun  * GNU General Public License for more details.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * You should have received a copy of the GNU General Public License
17*4882a593Smuzhiyun  * along with this program; if not, write to the Free Software
18*4882a593Smuzhiyun  * Foundation, Inc.,
19*4882a593Smuzhiyun  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20*4882a593Smuzhiyun  */
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include <crypto/scatterwalk.h>
23*4882a593Smuzhiyun #include <linux/scatterlist.h>
24*4882a593Smuzhiyun #include "util.h"
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun /* These were taken from Maxim Levitsky's patch to lkml.
27*4882a593Smuzhiyun  */
cryptodev_sg_advance(struct scatterlist * sg,int consumed)28*4882a593Smuzhiyun struct scatterlist *cryptodev_sg_advance(struct scatterlist *sg, int consumed)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun 	while (consumed >= sg->length) {
31*4882a593Smuzhiyun 		consumed -= sg->length;
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 		sg = sg_next(sg);
34*4882a593Smuzhiyun 		if (!sg)
35*4882a593Smuzhiyun 			break;
36*4882a593Smuzhiyun 	}
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	WARN_ON(!sg && consumed);
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	if (!sg)
41*4882a593Smuzhiyun 		return NULL;
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	sg->offset += consumed;
44*4882a593Smuzhiyun 	sg->length -= consumed;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	if (sg->offset >= PAGE_SIZE) {
47*4882a593Smuzhiyun 		struct page *page =
48*4882a593Smuzhiyun 			nth_page(sg_page(sg), sg->offset / PAGE_SIZE);
49*4882a593Smuzhiyun 		sg_set_page(sg, page, sg->length, sg->offset % PAGE_SIZE);
50*4882a593Smuzhiyun 	}
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	return sg;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun /**
56*4882a593Smuzhiyun  * cryptodev_sg_copy - copies sg entries from sg_from to sg_to, such
57*4882a593Smuzhiyun  * as sg_to covers first 'len' bytes from sg_from.
58*4882a593Smuzhiyun  */
cryptodev_sg_copy(struct scatterlist * sg_from,struct scatterlist * sg_to,int len)59*4882a593Smuzhiyun int cryptodev_sg_copy(struct scatterlist *sg_from, struct scatterlist *sg_to, int len)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun 	while (len > sg_from->length) {
62*4882a593Smuzhiyun 		len -= sg_from->length;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 		sg_set_page(sg_to, sg_page(sg_from),
65*4882a593Smuzhiyun 				sg_from->length, sg_from->offset);
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 		sg_to = sg_next(sg_to);
68*4882a593Smuzhiyun 		sg_from = sg_next(sg_from);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 		if (len && (!sg_from || !sg_to))
71*4882a593Smuzhiyun 			return -ENOMEM;
72*4882a593Smuzhiyun 	}
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	if (len)
75*4882a593Smuzhiyun 		sg_set_page(sg_to, sg_page(sg_from),
76*4882a593Smuzhiyun 				len, sg_from->offset);
77*4882a593Smuzhiyun 	sg_mark_end(sg_to);
78*4882a593Smuzhiyun 	return 0;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun 
81