18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Cryptographic scatter and gather helpers.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
68c2ecf20Sopenharmony_ci * Copyright (c) 2002 Adam J. Richter <adam@yggdrasil.com>
78c2ecf20Sopenharmony_ci * Copyright (c) 2004 Jean-Luc Cooke <jlcooke@certainkey.com>
88c2ecf20Sopenharmony_ci * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#ifndef _CRYPTO_SCATTERWALK_H
128c2ecf20Sopenharmony_ci#define _CRYPTO_SCATTERWALK_H
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <crypto/algapi.h>
158c2ecf20Sopenharmony_ci#include <linux/highmem.h>
168c2ecf20Sopenharmony_ci#include <linux/kernel.h>
178c2ecf20Sopenharmony_ci#include <linux/scatterlist.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic inline void scatterwalk_crypto_chain(struct scatterlist *head,
208c2ecf20Sopenharmony_ci					    struct scatterlist *sg, int num)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	if (sg)
238c2ecf20Sopenharmony_ci		sg_chain(head, num, sg);
248c2ecf20Sopenharmony_ci	else
258c2ecf20Sopenharmony_ci		sg_mark_end(head);
268c2ecf20Sopenharmony_ci}
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic inline unsigned int scatterwalk_pagelen(struct scatter_walk *walk)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	unsigned int len = walk->sg->offset + walk->sg->length - walk->offset;
318c2ecf20Sopenharmony_ci	unsigned int len_this_page = offset_in_page(~walk->offset) + 1;
328c2ecf20Sopenharmony_ci	return len_this_page > len ? len : len_this_page;
338c2ecf20Sopenharmony_ci}
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic inline unsigned int scatterwalk_clamp(struct scatter_walk *walk,
368c2ecf20Sopenharmony_ci					     unsigned int nbytes)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	unsigned int len_this_page = scatterwalk_pagelen(walk);
398c2ecf20Sopenharmony_ci	return nbytes > len_this_page ? len_this_page : nbytes;
408c2ecf20Sopenharmony_ci}
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic inline void scatterwalk_advance(struct scatter_walk *walk,
438c2ecf20Sopenharmony_ci				       unsigned int nbytes)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	walk->offset += nbytes;
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic inline unsigned int scatterwalk_aligned(struct scatter_walk *walk,
498c2ecf20Sopenharmony_ci					       unsigned int alignmask)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	return !(walk->offset & alignmask);
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic inline struct page *scatterwalk_page(struct scatter_walk *walk)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	return sg_page(walk->sg) + (walk->offset >> PAGE_SHIFT);
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic inline void scatterwalk_unmap(void *vaddr)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	kunmap_atomic(vaddr);
628c2ecf20Sopenharmony_ci}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic inline void scatterwalk_start(struct scatter_walk *walk,
658c2ecf20Sopenharmony_ci				     struct scatterlist *sg)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	walk->sg = sg;
688c2ecf20Sopenharmony_ci	walk->offset = sg->offset;
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic inline void *scatterwalk_map(struct scatter_walk *walk)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	return kmap_atomic(scatterwalk_page(walk)) +
748c2ecf20Sopenharmony_ci	       offset_in_page(walk->offset);
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic inline void scatterwalk_pagedone(struct scatter_walk *walk, int out,
788c2ecf20Sopenharmony_ci					unsigned int more)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	if (out) {
818c2ecf20Sopenharmony_ci		struct page *page;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci		page = sg_page(walk->sg) + ((walk->offset - 1) >> PAGE_SHIFT);
848c2ecf20Sopenharmony_ci		/* Test ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE first as
858c2ecf20Sopenharmony_ci		 * PageSlab cannot be optimised away per se due to
868c2ecf20Sopenharmony_ci		 * use of volatile pointer.
878c2ecf20Sopenharmony_ci		 */
888c2ecf20Sopenharmony_ci		if (ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE && !PageSlab(page))
898c2ecf20Sopenharmony_ci			flush_dcache_page(page);
908c2ecf20Sopenharmony_ci	}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	if (more && walk->offset >= walk->sg->offset + walk->sg->length)
938c2ecf20Sopenharmony_ci		scatterwalk_start(walk, sg_next(walk->sg));
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic inline void scatterwalk_done(struct scatter_walk *walk, int out,
978c2ecf20Sopenharmony_ci				    int more)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	if (!more || walk->offset >= walk->sg->offset + walk->sg->length ||
1008c2ecf20Sopenharmony_ci	    !(walk->offset & (PAGE_SIZE - 1)))
1018c2ecf20Sopenharmony_ci		scatterwalk_pagedone(walk, out, more);
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_civoid scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
1058c2ecf20Sopenharmony_ci			    size_t nbytes, int out);
1068c2ecf20Sopenharmony_civoid *scatterwalk_map(struct scatter_walk *walk);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_civoid scatterwalk_map_and_copy(void *buf, struct scatterlist *sg,
1098c2ecf20Sopenharmony_ci			      unsigned int start, unsigned int nbytes, int out);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistruct scatterlist *scatterwalk_ffwd(struct scatterlist dst[2],
1128c2ecf20Sopenharmony_ci				     struct scatterlist *src,
1138c2ecf20Sopenharmony_ci				     unsigned int len);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci#endif  /* _CRYPTO_SCATTERWALK_H */
116