162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Cryptographic scatter and gather helpers. 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> 662306a36Sopenharmony_ci * Copyright (c) 2002 Adam J. Richter <adam@yggdrasil.com> 762306a36Sopenharmony_ci * Copyright (c) 2004 Jean-Luc Cooke <jlcooke@certainkey.com> 862306a36Sopenharmony_ci * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au> 962306a36Sopenharmony_ci */ 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci#ifndef _CRYPTO_SCATTERWALK_H 1262306a36Sopenharmony_ci#define _CRYPTO_SCATTERWALK_H 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci#include <crypto/algapi.h> 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ci#include <linux/highmem.h> 1762306a36Sopenharmony_ci#include <linux/mm.h> 1862306a36Sopenharmony_ci#include <linux/scatterlist.h> 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_cistatic inline void scatterwalk_crypto_chain(struct scatterlist *head, 2162306a36Sopenharmony_ci struct scatterlist *sg, int num) 2262306a36Sopenharmony_ci{ 2362306a36Sopenharmony_ci if (sg) 2462306a36Sopenharmony_ci sg_chain(head, num, sg); 2562306a36Sopenharmony_ci else 2662306a36Sopenharmony_ci sg_mark_end(head); 2762306a36Sopenharmony_ci} 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_cistatic inline unsigned int scatterwalk_pagelen(struct scatter_walk *walk) 3062306a36Sopenharmony_ci{ 3162306a36Sopenharmony_ci unsigned int len = walk->sg->offset + walk->sg->length - walk->offset; 3262306a36Sopenharmony_ci unsigned int len_this_page = offset_in_page(~walk->offset) + 1; 3362306a36Sopenharmony_ci return len_this_page > len ? len : len_this_page; 3462306a36Sopenharmony_ci} 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_cistatic inline unsigned int scatterwalk_clamp(struct scatter_walk *walk, 3762306a36Sopenharmony_ci unsigned int nbytes) 3862306a36Sopenharmony_ci{ 3962306a36Sopenharmony_ci unsigned int len_this_page = scatterwalk_pagelen(walk); 4062306a36Sopenharmony_ci return nbytes > len_this_page ? len_this_page : nbytes; 4162306a36Sopenharmony_ci} 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_cistatic inline void scatterwalk_advance(struct scatter_walk *walk, 4462306a36Sopenharmony_ci unsigned int nbytes) 4562306a36Sopenharmony_ci{ 4662306a36Sopenharmony_ci walk->offset += nbytes; 4762306a36Sopenharmony_ci} 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_cistatic inline struct page *scatterwalk_page(struct scatter_walk *walk) 5062306a36Sopenharmony_ci{ 5162306a36Sopenharmony_ci return sg_page(walk->sg) + (walk->offset >> PAGE_SHIFT); 5262306a36Sopenharmony_ci} 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_cistatic inline void scatterwalk_unmap(void *vaddr) 5562306a36Sopenharmony_ci{ 5662306a36Sopenharmony_ci kunmap_local(vaddr); 5762306a36Sopenharmony_ci} 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_cistatic inline void scatterwalk_start(struct scatter_walk *walk, 6062306a36Sopenharmony_ci struct scatterlist *sg) 6162306a36Sopenharmony_ci{ 6262306a36Sopenharmony_ci walk->sg = sg; 6362306a36Sopenharmony_ci walk->offset = sg->offset; 6462306a36Sopenharmony_ci} 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_cistatic inline void *scatterwalk_map(struct scatter_walk *walk) 6762306a36Sopenharmony_ci{ 6862306a36Sopenharmony_ci return kmap_local_page(scatterwalk_page(walk)) + 6962306a36Sopenharmony_ci offset_in_page(walk->offset); 7062306a36Sopenharmony_ci} 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_cistatic inline void scatterwalk_pagedone(struct scatter_walk *walk, int out, 7362306a36Sopenharmony_ci unsigned int more) 7462306a36Sopenharmony_ci{ 7562306a36Sopenharmony_ci if (out) { 7662306a36Sopenharmony_ci struct page *page; 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci page = sg_page(walk->sg) + ((walk->offset - 1) >> PAGE_SHIFT); 7962306a36Sopenharmony_ci flush_dcache_page(page); 8062306a36Sopenharmony_ci } 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci if (more && walk->offset >= walk->sg->offset + walk->sg->length) 8362306a36Sopenharmony_ci scatterwalk_start(walk, sg_next(walk->sg)); 8462306a36Sopenharmony_ci} 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_cistatic inline void scatterwalk_done(struct scatter_walk *walk, int out, 8762306a36Sopenharmony_ci int more) 8862306a36Sopenharmony_ci{ 8962306a36Sopenharmony_ci if (!more || walk->offset >= walk->sg->offset + walk->sg->length || 9062306a36Sopenharmony_ci !(walk->offset & (PAGE_SIZE - 1))) 9162306a36Sopenharmony_ci scatterwalk_pagedone(walk, out, more); 9262306a36Sopenharmony_ci} 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_civoid scatterwalk_copychunks(void *buf, struct scatter_walk *walk, 9562306a36Sopenharmony_ci size_t nbytes, int out); 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_civoid scatterwalk_map_and_copy(void *buf, struct scatterlist *sg, 9862306a36Sopenharmony_ci unsigned int start, unsigned int nbytes, int out); 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_cistruct scatterlist *scatterwalk_ffwd(struct scatterlist dst[2], 10162306a36Sopenharmony_ci struct scatterlist *src, 10262306a36Sopenharmony_ci unsigned int len); 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_ci#endif /* _CRYPTO_SCATTERWALK_H */ 105