18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright 2015-2016 Freescale Semiconductor, Inc.
48c2ecf20Sopenharmony_ci * Copyright 2017 NXP
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#ifndef _SG_SW_QM2_H_
88c2ecf20Sopenharmony_ci#define _SG_SW_QM2_H_
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <soc/fsl/dpaa2-fd.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cistatic inline void dma_to_qm_sg_one(struct dpaa2_sg_entry *qm_sg_ptr,
138c2ecf20Sopenharmony_ci				    dma_addr_t dma, u32 len, u16 offset)
148c2ecf20Sopenharmony_ci{
158c2ecf20Sopenharmony_ci	dpaa2_sg_set_addr(qm_sg_ptr, dma);
168c2ecf20Sopenharmony_ci	dpaa2_sg_set_format(qm_sg_ptr, dpaa2_sg_single);
178c2ecf20Sopenharmony_ci	dpaa2_sg_set_final(qm_sg_ptr, false);
188c2ecf20Sopenharmony_ci	dpaa2_sg_set_len(qm_sg_ptr, len);
198c2ecf20Sopenharmony_ci	dpaa2_sg_set_bpid(qm_sg_ptr, 0);
208c2ecf20Sopenharmony_ci	dpaa2_sg_set_offset(qm_sg_ptr, offset);
218c2ecf20Sopenharmony_ci}
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/*
248c2ecf20Sopenharmony_ci * convert scatterlist to h/w link table format
258c2ecf20Sopenharmony_ci * but does not have final bit; instead, returns last entry
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_cistatic inline struct dpaa2_sg_entry *
288c2ecf20Sopenharmony_cisg_to_qm_sg(struct scatterlist *sg, int len,
298c2ecf20Sopenharmony_ci	    struct dpaa2_sg_entry *qm_sg_ptr, u16 offset)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	int ent_len;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	while (len) {
348c2ecf20Sopenharmony_ci		ent_len = min_t(int, sg_dma_len(sg), len);
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci		dma_to_qm_sg_one(qm_sg_ptr, sg_dma_address(sg), ent_len,
378c2ecf20Sopenharmony_ci				 offset);
388c2ecf20Sopenharmony_ci		qm_sg_ptr++;
398c2ecf20Sopenharmony_ci		sg = sg_next(sg);
408c2ecf20Sopenharmony_ci		len -= ent_len;
418c2ecf20Sopenharmony_ci	}
428c2ecf20Sopenharmony_ci	return qm_sg_ptr - 1;
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/*
468c2ecf20Sopenharmony_ci * convert scatterlist to h/w link table format
478c2ecf20Sopenharmony_ci * scatterlist must have been previously dma mapped
488c2ecf20Sopenharmony_ci */
498c2ecf20Sopenharmony_cistatic inline void sg_to_qm_sg_last(struct scatterlist *sg, int len,
508c2ecf20Sopenharmony_ci				    struct dpaa2_sg_entry *qm_sg_ptr,
518c2ecf20Sopenharmony_ci				    u16 offset)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	qm_sg_ptr = sg_to_qm_sg(sg, len, qm_sg_ptr, offset);
548c2ecf20Sopenharmony_ci	dpaa2_sg_set_final(qm_sg_ptr, true);
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci#endif /* _SG_SW_QM2_H_ */
58