Path: blob/master/drivers/crypto/marvell/octeontx2/cn10k_cpt.c
26285 views
// SPDX-License-Identifier: GPL-2.0-only1/* Copyright (C) 2021 Marvell. */23#include <linux/soc/marvell/octeontx2/asm.h>4#include "otx2_cptpf.h"5#include "otx2_cptvf.h"6#include "otx2_cptlf.h"7#include "cn10k_cpt.h"8#include "otx2_cpt_common.h"910static void cn10k_cpt_send_cmd(union otx2_cpt_inst_s *cptinst, u32 insts_num,11struct otx2_cptlf_info *lf);1213static struct cpt_hw_ops otx2_hw_ops = {14.send_cmd = otx2_cpt_send_cmd,15.cpt_get_compcode = otx2_cpt_get_compcode,16.cpt_get_uc_compcode = otx2_cpt_get_uc_compcode,17.cpt_sg_info_create = otx2_sg_info_create,18};1920static struct cpt_hw_ops cn10k_hw_ops = {21.send_cmd = cn10k_cpt_send_cmd,22.cpt_get_compcode = cn10k_cpt_get_compcode,23.cpt_get_uc_compcode = cn10k_cpt_get_uc_compcode,24.cpt_sg_info_create = otx2_sg_info_create,25};2627static void cn10k_cpt_send_cmd(union otx2_cpt_inst_s *cptinst, u32 insts_num,28struct otx2_cptlf_info *lf)29{30void *lmtline = lf->lfs->lmt_info.base + (lf->slot * LMTLINE_SIZE);31u64 val = (lf->slot & 0x7FF);32u64 tar_addr = 0;3334/* tar_addr<6:4> = Size of first LMTST - 1 in units of 128b. */35tar_addr |= (__force u64)lf->ioreg |36(((OTX2_CPT_INST_SIZE/16) - 1) & 0x7) << 4;37/*38* Make sure memory areas pointed in CPT_INST_S39* are flushed before the instruction is sent to CPT40*/41dma_wmb();4243/* Copy CPT command to LMTLINE */44memcpy(lmtline, cptinst, insts_num * OTX2_CPT_INST_SIZE);45cn10k_lmt_flush(val, tar_addr);46}4748void cn10k_cpt_lmtst_free(struct pci_dev *pdev, struct otx2_cptlfs_info *lfs)49{50struct otx2_lmt_info *lmt_info = &lfs->lmt_info;5152if (!lmt_info->base)53return;5455dma_free_attrs(&pdev->dev, lmt_info->size,56lmt_info->base - lmt_info->align,57lmt_info->iova - lmt_info->align,58DMA_ATTR_FORCE_CONTIGUOUS);59}60EXPORT_SYMBOL_NS_GPL(cn10k_cpt_lmtst_free, "CRYPTO_DEV_OCTEONTX2_CPT");6162static int cn10k_cpt_lmtst_alloc(struct pci_dev *pdev,63struct otx2_cptlfs_info *lfs, u32 size)64{65struct otx2_lmt_info *lmt_info = &lfs->lmt_info;66dma_addr_t align_iova;67dma_addr_t iova;6869lmt_info->base = dma_alloc_attrs(&pdev->dev, size, &iova, GFP_KERNEL,70DMA_ATTR_FORCE_CONTIGUOUS);71if (!lmt_info->base)72return -ENOMEM;7374align_iova = ALIGN((u64)iova, LMTLINE_ALIGN);75lmt_info->iova = align_iova;76lmt_info->align = align_iova - iova;77lmt_info->size = size;78lmt_info->base += lmt_info->align;79return 0;80}8182int cn10k_cptpf_lmtst_init(struct otx2_cptpf_dev *cptpf)83{84struct pci_dev *pdev = cptpf->pdev;85u32 size;86int ret;8788if (!test_bit(CN10K_LMTST, &cptpf->cap_flag)) {89cptpf->lfs.ops = &otx2_hw_ops;90return 0;91}9293cptpf->lfs.ops = &cn10k_hw_ops;94size = OTX2_CPT_MAX_VFS_NUM * LMTLINE_SIZE + LMTLINE_ALIGN;95ret = cn10k_cpt_lmtst_alloc(pdev, &cptpf->lfs, size);96if (ret) {97dev_err(&pdev->dev, "PF-%d LMTLINE memory allocation failed\n",98cptpf->pf_id);99return ret;100}101102ret = otx2_cpt_lmtst_tbl_setup_msg(&cptpf->lfs);103if (ret) {104dev_err(&pdev->dev, "PF-%d: LMTST Table setup failed\n",105cptpf->pf_id);106cn10k_cpt_lmtst_free(pdev, &cptpf->lfs);107}108109return 0;110}111EXPORT_SYMBOL_NS_GPL(cn10k_cptpf_lmtst_init, "CRYPTO_DEV_OCTEONTX2_CPT");112113int cn10k_cptvf_lmtst_init(struct otx2_cptvf_dev *cptvf)114{115struct pci_dev *pdev = cptvf->pdev;116u32 size;117int ret;118119if (!test_bit(CN10K_LMTST, &cptvf->cap_flag))120return 0;121122size = cptvf->lfs.lfs_num * LMTLINE_SIZE + LMTLINE_ALIGN;123ret = cn10k_cpt_lmtst_alloc(pdev, &cptvf->lfs, size);124if (ret) {125dev_err(&pdev->dev, "VF-%d LMTLINE memory allocation failed\n",126cptvf->vf_id);127return ret;128}129130ret = otx2_cpt_lmtst_tbl_setup_msg(&cptvf->lfs);131if (ret) {132dev_err(&pdev->dev, "VF-%d: LMTST Table setup failed\n",133cptvf->vf_id);134cn10k_cpt_lmtst_free(pdev, &cptvf->lfs);135}136137return 0;138}139EXPORT_SYMBOL_NS_GPL(cn10k_cptvf_lmtst_init, "CRYPTO_DEV_OCTEONTX2_CPT");140141void cn10k_cpt_hw_ctx_clear(struct pci_dev *pdev,142struct cn10k_cpt_errata_ctx *er_ctx)143{144u64 cptr_dma;145146if (!is_dev_cn10ka_ax(pdev))147return;148149cptr_dma = er_ctx->cptr_dma & ~(BIT_ULL(60));150cn10k_cpt_ctx_flush(pdev, cptr_dma, true);151dma_unmap_single(&pdev->dev, cptr_dma, CN10K_CPT_HW_CTX_SIZE,152DMA_BIDIRECTIONAL);153kfree(er_ctx->hw_ctx);154}155EXPORT_SYMBOL_NS_GPL(cn10k_cpt_hw_ctx_clear, "CRYPTO_DEV_OCTEONTX2_CPT");156157void cn10k_cpt_hw_ctx_set(union cn10k_cpt_hw_ctx *hctx, u16 ctx_sz)158{159hctx->w0.aop_valid = 1;160hctx->w0.ctx_hdr_sz = 0;161hctx->w0.ctx_sz = ctx_sz;162hctx->w0.ctx_push_sz = 1;163}164EXPORT_SYMBOL_NS_GPL(cn10k_cpt_hw_ctx_set, "CRYPTO_DEV_OCTEONTX2_CPT");165166int cn10k_cpt_hw_ctx_init(struct pci_dev *pdev,167struct cn10k_cpt_errata_ctx *er_ctx)168{169union cn10k_cpt_hw_ctx *hctx;170u64 cptr_dma;171172er_ctx->cptr_dma = 0;173er_ctx->hw_ctx = NULL;174175if (!is_dev_cn10ka_ax(pdev))176return 0;177178hctx = kmalloc(CN10K_CPT_HW_CTX_SIZE, GFP_KERNEL);179if (unlikely(!hctx))180return -ENOMEM;181cptr_dma = dma_map_single(&pdev->dev, hctx, CN10K_CPT_HW_CTX_SIZE,182DMA_BIDIRECTIONAL);183if (dma_mapping_error(&pdev->dev, cptr_dma)) {184kfree(hctx);185return -ENOMEM;186}187188cn10k_cpt_hw_ctx_set(hctx, 1);189er_ctx->hw_ctx = hctx;190er_ctx->cptr_dma = cptr_dma | BIT_ULL(60);191192return 0;193}194EXPORT_SYMBOL_NS_GPL(cn10k_cpt_hw_ctx_init, "CRYPTO_DEV_OCTEONTX2_CPT");195196void cn10k_cpt_ctx_flush(struct pci_dev *pdev, u64 cptr, bool inval)197{198struct otx2_cptvf_dev *cptvf = pci_get_drvdata(pdev);199struct otx2_cptlfs_info *lfs = &cptvf->lfs;200u64 reg;201202reg = (uintptr_t)cptr >> 7;203if (inval)204reg = reg | BIT_ULL(46);205206otx2_cpt_write64(lfs->reg_base, lfs->blkaddr, lfs->lf[0].slot,207OTX2_CPT_LF_CTX_FLUSH, reg);208/* Make sure that the FLUSH operation is complete */209wmb();210otx2_cpt_read64(lfs->reg_base, lfs->blkaddr, lfs->lf[0].slot,211OTX2_CPT_LF_CTX_ERR);212}213EXPORT_SYMBOL_NS_GPL(cn10k_cpt_ctx_flush, "CRYPTO_DEV_OCTEONTX2_CPT");214215void cptvf_hw_ops_get(struct otx2_cptvf_dev *cptvf)216{217if (test_bit(CN10K_LMTST, &cptvf->cap_flag))218cptvf->lfs.ops = &cn10k_hw_ops;219else220cptvf->lfs.ops = &otx2_hw_ops;221}222EXPORT_SYMBOL_NS_GPL(cptvf_hw_ops_get, "CRYPTO_DEV_OCTEONTX2_CPT");223224225