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