18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright 2013-2016 Freescale Semiconductor, Inc.
48c2ecf20Sopenharmony_ci * Copyright 2016-2017 NXP
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#ifndef __SG_SW_QM_H
88c2ecf20Sopenharmony_ci#define __SG_SW_QM_H
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <soc/fsl/qman.h>
118c2ecf20Sopenharmony_ci#include "regs.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistatic inline void __dma_to_qm_sg(struct qm_sg_entry *qm_sg_ptr, dma_addr_t dma,
148c2ecf20Sopenharmony_ci				  u16 offset)
158c2ecf20Sopenharmony_ci{
168c2ecf20Sopenharmony_ci	qm_sg_entry_set64(qm_sg_ptr, dma);
178c2ecf20Sopenharmony_ci	qm_sg_ptr->__reserved2 = 0;
188c2ecf20Sopenharmony_ci	qm_sg_ptr->bpid = 0;
198c2ecf20Sopenharmony_ci	qm_sg_ptr->offset = cpu_to_be16(offset & QM_SG_OFF_MASK);
208c2ecf20Sopenharmony_ci}
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic inline void dma_to_qm_sg_one(struct qm_sg_entry *qm_sg_ptr,
238c2ecf20Sopenharmony_ci				    dma_addr_t dma, u32 len, u16 offset)
248c2ecf20Sopenharmony_ci{
258c2ecf20Sopenharmony_ci	__dma_to_qm_sg(qm_sg_ptr, dma, offset);
268c2ecf20Sopenharmony_ci	qm_sg_entry_set_len(qm_sg_ptr, len);
278c2ecf20Sopenharmony_ci}
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic inline void dma_to_qm_sg_one_last(struct qm_sg_entry *qm_sg_ptr,
308c2ecf20Sopenharmony_ci					 dma_addr_t dma, u32 len, u16 offset)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	__dma_to_qm_sg(qm_sg_ptr, dma, offset);
338c2ecf20Sopenharmony_ci	qm_sg_entry_set_f(qm_sg_ptr, len);
348c2ecf20Sopenharmony_ci}
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistatic inline void dma_to_qm_sg_one_ext(struct qm_sg_entry *qm_sg_ptr,
378c2ecf20Sopenharmony_ci					dma_addr_t dma, u32 len, u16 offset)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	__dma_to_qm_sg(qm_sg_ptr, dma, offset);
408c2ecf20Sopenharmony_ci	qm_sg_ptr->cfg = cpu_to_be32(QM_SG_EXT | (len & QM_SG_LEN_MASK));
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic inline void dma_to_qm_sg_one_last_ext(struct qm_sg_entry *qm_sg_ptr,
448c2ecf20Sopenharmony_ci					     dma_addr_t dma, u32 len,
458c2ecf20Sopenharmony_ci					     u16 offset)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	__dma_to_qm_sg(qm_sg_ptr, dma, offset);
488c2ecf20Sopenharmony_ci	qm_sg_ptr->cfg = cpu_to_be32(QM_SG_EXT | QM_SG_FIN |
498c2ecf20Sopenharmony_ci				     (len & QM_SG_LEN_MASK));
508c2ecf20Sopenharmony_ci}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/*
538c2ecf20Sopenharmony_ci * convert scatterlist to h/w link table format
548c2ecf20Sopenharmony_ci * but does not have final bit; instead, returns last entry
558c2ecf20Sopenharmony_ci */
568c2ecf20Sopenharmony_cistatic inline struct qm_sg_entry *
578c2ecf20Sopenharmony_cisg_to_qm_sg(struct scatterlist *sg, int len,
588c2ecf20Sopenharmony_ci	    struct qm_sg_entry *qm_sg_ptr, u16 offset)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	int ent_len;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	while (len) {
638c2ecf20Sopenharmony_ci		ent_len = min_t(int, sg_dma_len(sg), len);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci		dma_to_qm_sg_one(qm_sg_ptr, sg_dma_address(sg), ent_len,
668c2ecf20Sopenharmony_ci				 offset);
678c2ecf20Sopenharmony_ci		qm_sg_ptr++;
688c2ecf20Sopenharmony_ci		sg = sg_next(sg);
698c2ecf20Sopenharmony_ci		len -= ent_len;
708c2ecf20Sopenharmony_ci	}
718c2ecf20Sopenharmony_ci	return qm_sg_ptr - 1;
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/*
758c2ecf20Sopenharmony_ci * convert scatterlist to h/w link table format
768c2ecf20Sopenharmony_ci * scatterlist must have been previously dma mapped
778c2ecf20Sopenharmony_ci */
788c2ecf20Sopenharmony_cistatic inline void sg_to_qm_sg_last(struct scatterlist *sg, int len,
798c2ecf20Sopenharmony_ci				    struct qm_sg_entry *qm_sg_ptr, u16 offset)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	qm_sg_ptr = sg_to_qm_sg(sg, len, qm_sg_ptr, offset);
828c2ecf20Sopenharmony_ci	qm_sg_entry_set_f(qm_sg_ptr, qm_sg_entry_get_len(qm_sg_ptr));
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci#endif /* __SG_SW_QM_H */
86