Path: blob/master/drivers/crypto/marvell/octeontx2/otx2_cptpf_main.c
26292 views
// SPDX-License-Identifier: GPL-2.0-only1/* Copyright (C) 2020 Marvell. */23#include <linux/firmware.h>4#include "otx2_cpt_hw_types.h"5#include "otx2_cpt_common.h"6#include "otx2_cpt_devlink.h"7#include "otx2_cptpf_ucode.h"8#include "otx2_cptpf.h"9#include "cn10k_cpt.h"10#include "rvu_reg.h"1112#define OTX2_CPT_DRV_NAME "rvu_cptpf"13#define OTX2_CPT_DRV_STRING "Marvell RVU CPT Physical Function Driver"1415#define CPT_UC_RID_CN9K_B0 116#define CPT_UC_RID_CN10K_A 417#define CPT_UC_RID_CN10K_B 51819static void cptpf_enable_vfpf_mbox_intr(struct otx2_cptpf_dev *cptpf,20int num_vfs)21{22int ena_bits;2324/* Clear any pending interrupts */25otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,26RVU_PF_VFPF_MBOX_INTX(0), ~0x0ULL);27otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,28RVU_PF_VFPF_MBOX_INTX(1), ~0x0ULL);2930/* Enable VF interrupts for VFs from 0 to 63 */31ena_bits = ((num_vfs - 1) % 64);32otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,33RVU_PF_VFPF_MBOX_INT_ENA_W1SX(0),34GENMASK_ULL(ena_bits, 0));3536if (num_vfs > 64) {37/* Enable VF interrupts for VFs from 64 to 127 */38ena_bits = num_vfs - 64 - 1;39otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,40RVU_PF_VFPF_MBOX_INT_ENA_W1SX(1),41GENMASK_ULL(ena_bits, 0));42}43}4445static void cptpf_disable_vfpf_mbox_intr(struct otx2_cptpf_dev *cptpf,46int num_vfs)47{48int vector;4950/* Disable VF-PF interrupts */51otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,52RVU_PF_VFPF_MBOX_INT_ENA_W1CX(0), ~0ULL);53otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,54RVU_PF_VFPF_MBOX_INT_ENA_W1CX(1), ~0ULL);55/* Clear any pending interrupts */56otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,57RVU_PF_VFPF_MBOX_INTX(0), ~0ULL);5859vector = pci_irq_vector(cptpf->pdev, RVU_PF_INT_VEC_VFPF_MBOX0);60free_irq(vector, cptpf);6162if (num_vfs > 64) {63otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,64RVU_PF_VFPF_MBOX_INTX(1), ~0ULL);65vector = pci_irq_vector(cptpf->pdev, RVU_PF_INT_VEC_VFPF_MBOX1);66free_irq(vector, cptpf);67}68}6970static void cptpf_enable_vf_flr_me_intrs(struct otx2_cptpf_dev *cptpf,71int num_vfs)72{73/* Clear FLR interrupt if any */74otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0, RVU_PF_VFFLR_INTX(0),75INTR_MASK(num_vfs));7677/* Enable VF FLR interrupts */78otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,79RVU_PF_VFFLR_INT_ENA_W1SX(0), INTR_MASK(num_vfs));80/* Clear ME interrupt if any */81otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0, RVU_PF_VFME_INTX(0),82INTR_MASK(num_vfs));83/* Enable VF ME interrupts */84otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,85RVU_PF_VFME_INT_ENA_W1SX(0), INTR_MASK(num_vfs));8687if (num_vfs <= 64)88return;8990otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0, RVU_PF_VFFLR_INTX(1),91INTR_MASK(num_vfs - 64));92otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,93RVU_PF_VFFLR_INT_ENA_W1SX(1), INTR_MASK(num_vfs - 64));9495otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0, RVU_PF_VFME_INTX(1),96INTR_MASK(num_vfs - 64));97otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,98RVU_PF_VFME_INT_ENA_W1SX(1), INTR_MASK(num_vfs - 64));99}100101static void cptpf_disable_vf_flr_me_intrs(struct otx2_cptpf_dev *cptpf,102int num_vfs)103{104int vector;105106/* Disable VF FLR interrupts */107otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,108RVU_PF_VFFLR_INT_ENA_W1CX(0), INTR_MASK(num_vfs));109vector = pci_irq_vector(cptpf->pdev, RVU_PF_INT_VEC_VFFLR0);110free_irq(vector, cptpf);111112/* Disable VF ME interrupts */113otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,114RVU_PF_VFME_INT_ENA_W1CX(0), INTR_MASK(num_vfs));115vector = pci_irq_vector(cptpf->pdev, RVU_PF_INT_VEC_VFME0);116free_irq(vector, cptpf);117118if (num_vfs <= 64)119return;120121otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,122RVU_PF_VFFLR_INT_ENA_W1CX(1), INTR_MASK(num_vfs - 64));123vector = pci_irq_vector(cptpf->pdev, RVU_PF_INT_VEC_VFFLR1);124free_irq(vector, cptpf);125126otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,127RVU_PF_VFME_INT_ENA_W1CX(1), INTR_MASK(num_vfs - 64));128vector = pci_irq_vector(cptpf->pdev, RVU_PF_INT_VEC_VFME1);129free_irq(vector, cptpf);130}131132static void cptpf_flr_wq_handler(struct work_struct *work)133{134struct cptpf_flr_work *flr_work;135struct otx2_cptpf_dev *pf;136struct mbox_msghdr *req;137struct otx2_mbox *mbox;138int vf, reg = 0;139140flr_work = container_of(work, struct cptpf_flr_work, work);141pf = flr_work->pf;142mbox = &pf->afpf_mbox;143144vf = flr_work - pf->flr_work;145146mutex_lock(&pf->lock);147req = otx2_mbox_alloc_msg_rsp(mbox, 0, sizeof(*req),148sizeof(struct msg_rsp));149if (!req) {150mutex_unlock(&pf->lock);151return;152}153154req->sig = OTX2_MBOX_REQ_SIG;155req->id = MBOX_MSG_VF_FLR;156req->pcifunc &= RVU_PFVF_FUNC_MASK;157req->pcifunc |= (vf + 1) & RVU_PFVF_FUNC_MASK;158159otx2_cpt_send_mbox_msg(mbox, pf->pdev);160if (!otx2_cpt_sync_mbox_msg(&pf->afpf_mbox)) {161162if (vf >= 64) {163reg = 1;164vf = vf - 64;165}166/* Clear transaction pending register */167otx2_cpt_write64(pf->reg_base, BLKADDR_RVUM, 0,168RVU_PF_VFTRPENDX(reg), BIT_ULL(vf));169otx2_cpt_write64(pf->reg_base, BLKADDR_RVUM, 0,170RVU_PF_VFFLR_INT_ENA_W1SX(reg), BIT_ULL(vf));171}172mutex_unlock(&pf->lock);173}174175static irqreturn_t cptpf_vf_flr_intr(int __always_unused irq, void *arg)176{177int reg, dev, vf, start_vf, num_reg = 1;178struct otx2_cptpf_dev *cptpf = arg;179u64 intr;180181if (cptpf->max_vfs > 64)182num_reg = 2;183184for (reg = 0; reg < num_reg; reg++) {185intr = otx2_cpt_read64(cptpf->reg_base, BLKADDR_RVUM, 0,186RVU_PF_VFFLR_INTX(reg));187if (!intr)188continue;189start_vf = 64 * reg;190for (vf = 0; vf < 64; vf++) {191if (!(intr & BIT_ULL(vf)))192continue;193dev = vf + start_vf;194queue_work(cptpf->flr_wq, &cptpf->flr_work[dev].work);195/* Clear interrupt */196otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,197RVU_PF_VFFLR_INTX(reg), BIT_ULL(vf));198/* Disable the interrupt */199otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,200RVU_PF_VFFLR_INT_ENA_W1CX(reg),201BIT_ULL(vf));202}203}204return IRQ_HANDLED;205}206207static irqreturn_t cptpf_vf_me_intr(int __always_unused irq, void *arg)208{209struct otx2_cptpf_dev *cptpf = arg;210int reg, vf, num_reg = 1;211u64 intr;212213if (cptpf->max_vfs > 64)214num_reg = 2;215216for (reg = 0; reg < num_reg; reg++) {217intr = otx2_cpt_read64(cptpf->reg_base, BLKADDR_RVUM, 0,218RVU_PF_VFME_INTX(reg));219if (!intr)220continue;221for (vf = 0; vf < 64; vf++) {222if (!(intr & BIT_ULL(vf)))223continue;224otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,225RVU_PF_VFTRPENDX(reg), BIT_ULL(vf));226/* Clear interrupt */227otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,228RVU_PF_VFME_INTX(reg), BIT_ULL(vf));229}230}231return IRQ_HANDLED;232}233234static void cptpf_unregister_vfpf_intr(struct otx2_cptpf_dev *cptpf,235int num_vfs)236{237cptpf_disable_vfpf_mbox_intr(cptpf, num_vfs);238cptpf_disable_vf_flr_me_intrs(cptpf, num_vfs);239}240241static int cptpf_register_vfpf_intr(struct otx2_cptpf_dev *cptpf, int num_vfs)242{243struct pci_dev *pdev = cptpf->pdev;244struct device *dev = &pdev->dev;245int ret, vector;246247vector = pci_irq_vector(pdev, RVU_PF_INT_VEC_VFPF_MBOX0);248/* Register VF-PF mailbox interrupt handler */249ret = request_irq(vector, otx2_cptpf_vfpf_mbox_intr, 0, "CPTVFPF Mbox0",250cptpf);251if (ret) {252dev_err(dev,253"IRQ registration failed for PFVF mbox0 irq\n");254return ret;255}256vector = pci_irq_vector(pdev, RVU_PF_INT_VEC_VFFLR0);257/* Register VF FLR interrupt handler */258ret = request_irq(vector, cptpf_vf_flr_intr, 0, "CPTPF FLR0", cptpf);259if (ret) {260dev_err(dev,261"IRQ registration failed for VFFLR0 irq\n");262goto free_mbox0_irq;263}264vector = pci_irq_vector(pdev, RVU_PF_INT_VEC_VFME0);265/* Register VF ME interrupt handler */266ret = request_irq(vector, cptpf_vf_me_intr, 0, "CPTPF ME0", cptpf);267if (ret) {268dev_err(dev,269"IRQ registration failed for PFVF mbox0 irq\n");270goto free_flr0_irq;271}272273if (num_vfs > 64) {274vector = pci_irq_vector(pdev, RVU_PF_INT_VEC_VFPF_MBOX1);275ret = request_irq(vector, otx2_cptpf_vfpf_mbox_intr, 0,276"CPTVFPF Mbox1", cptpf);277if (ret) {278dev_err(dev,279"IRQ registration failed for PFVF mbox1 irq\n");280goto free_me0_irq;281}282vector = pci_irq_vector(pdev, RVU_PF_INT_VEC_VFFLR1);283/* Register VF FLR interrupt handler */284ret = request_irq(vector, cptpf_vf_flr_intr, 0, "CPTPF FLR1",285cptpf);286if (ret) {287dev_err(dev,288"IRQ registration failed for VFFLR1 irq\n");289goto free_mbox1_irq;290}291vector = pci_irq_vector(pdev, RVU_PF_INT_VEC_VFME1);292/* Register VF FLR interrupt handler */293ret = request_irq(vector, cptpf_vf_me_intr, 0, "CPTPF ME1",294cptpf);295if (ret) {296dev_err(dev,297"IRQ registration failed for VFFLR1 irq\n");298goto free_flr1_irq;299}300}301cptpf_enable_vfpf_mbox_intr(cptpf, num_vfs);302cptpf_enable_vf_flr_me_intrs(cptpf, num_vfs);303304return 0;305306free_flr1_irq:307vector = pci_irq_vector(pdev, RVU_PF_INT_VEC_VFFLR1);308free_irq(vector, cptpf);309free_mbox1_irq:310vector = pci_irq_vector(pdev, RVU_PF_INT_VEC_VFPF_MBOX1);311free_irq(vector, cptpf);312free_me0_irq:313vector = pci_irq_vector(pdev, RVU_PF_INT_VEC_VFME0);314free_irq(vector, cptpf);315free_flr0_irq:316vector = pci_irq_vector(pdev, RVU_PF_INT_VEC_VFFLR0);317free_irq(vector, cptpf);318free_mbox0_irq:319vector = pci_irq_vector(pdev, RVU_PF_INT_VEC_VFPF_MBOX0);320free_irq(vector, cptpf);321return ret;322}323324static void cptpf_flr_wq_destroy(struct otx2_cptpf_dev *pf)325{326if (!pf->flr_wq)327return;328destroy_workqueue(pf->flr_wq);329pf->flr_wq = NULL;330kfree(pf->flr_work);331}332333static int cptpf_flr_wq_init(struct otx2_cptpf_dev *cptpf, int num_vfs)334{335int vf;336337cptpf->flr_wq = alloc_ordered_workqueue("cptpf_flr_wq", 0);338if (!cptpf->flr_wq)339return -ENOMEM;340341cptpf->flr_work = kcalloc(num_vfs, sizeof(struct cptpf_flr_work),342GFP_KERNEL);343if (!cptpf->flr_work)344goto destroy_wq;345346for (vf = 0; vf < num_vfs; vf++) {347cptpf->flr_work[vf].pf = cptpf;348INIT_WORK(&cptpf->flr_work[vf].work, cptpf_flr_wq_handler);349}350return 0;351352destroy_wq:353destroy_workqueue(cptpf->flr_wq);354return -ENOMEM;355}356357static int cptpf_vfpf_mbox_init(struct otx2_cptpf_dev *cptpf, int num_vfs)358{359struct device *dev = &cptpf->pdev->dev;360u64 vfpf_mbox_base;361int err, i;362363cptpf->vfpf_mbox_wq =364alloc_ordered_workqueue("cpt_vfpf_mailbox",365WQ_HIGHPRI | WQ_MEM_RECLAIM);366if (!cptpf->vfpf_mbox_wq)367return -ENOMEM;368369/* Map VF-PF mailbox memory */370if (test_bit(CN10K_MBOX, &cptpf->cap_flag))371vfpf_mbox_base = readq(cptpf->reg_base + RVU_PF_VF_MBOX_ADDR);372else373vfpf_mbox_base = readq(cptpf->reg_base + RVU_PF_VF_BAR4_ADDR);374375if (!vfpf_mbox_base) {376dev_err(dev, "VF-PF mailbox address not configured\n");377err = -ENOMEM;378goto free_wqe;379}380cptpf->vfpf_mbox_base = devm_ioremap_wc(dev, vfpf_mbox_base,381MBOX_SIZE * cptpf->max_vfs);382if (!cptpf->vfpf_mbox_base) {383dev_err(dev, "Mapping of VF-PF mailbox address failed\n");384err = -ENOMEM;385goto free_wqe;386}387err = otx2_mbox_init(&cptpf->vfpf_mbox, cptpf->vfpf_mbox_base,388cptpf->pdev, cptpf->reg_base, MBOX_DIR_PFVF,389num_vfs);390if (err)391goto free_wqe;392393for (i = 0; i < num_vfs; i++) {394cptpf->vf[i].vf_id = i;395cptpf->vf[i].cptpf = cptpf;396cptpf->vf[i].intr_idx = i % 64;397INIT_WORK(&cptpf->vf[i].vfpf_mbox_work,398otx2_cptpf_vfpf_mbox_handler);399}400return 0;401402free_wqe:403destroy_workqueue(cptpf->vfpf_mbox_wq);404return err;405}406407static void cptpf_vfpf_mbox_destroy(struct otx2_cptpf_dev *cptpf)408{409destroy_workqueue(cptpf->vfpf_mbox_wq);410otx2_mbox_destroy(&cptpf->vfpf_mbox);411}412413static void cptpf_disable_afpf_mbox_intr(struct otx2_cptpf_dev *cptpf)414{415/* Disable AF-PF interrupt */416otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0, RVU_PF_INT_ENA_W1C,4170x1ULL);418/* Clear interrupt if any */419otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0, RVU_PF_INT, 0x1ULL);420}421422static int cptpf_register_afpf_mbox_intr(struct otx2_cptpf_dev *cptpf)423{424struct pci_dev *pdev = cptpf->pdev;425struct device *dev = &pdev->dev;426int ret, irq;427428irq = pci_irq_vector(pdev, RVU_PF_INT_VEC_AFPF_MBOX);429/* Register AF-PF mailbox interrupt handler */430ret = devm_request_irq(dev, irq, otx2_cptpf_afpf_mbox_intr, 0,431"CPTAFPF Mbox", cptpf);432if (ret) {433dev_err(dev,434"IRQ registration failed for PFAF mbox irq\n");435return ret;436}437/* Clear interrupt if any, to avoid spurious interrupts */438otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0, RVU_PF_INT, 0x1ULL);439/* Enable AF-PF interrupt */440otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0, RVU_PF_INT_ENA_W1S,4410x1ULL);442443ret = otx2_cpt_send_ready_msg(&cptpf->afpf_mbox, cptpf->pdev);444if (ret) {445dev_warn(dev,446"AF not responding to mailbox, deferring probe\n");447cptpf_disable_afpf_mbox_intr(cptpf);448return -EPROBE_DEFER;449}450return 0;451}452453static int cptpf_afpf_mbox_init(struct otx2_cptpf_dev *cptpf)454{455struct pci_dev *pdev = cptpf->pdev;456resource_size_t offset;457int err;458459cptpf->afpf_mbox_wq =460alloc_ordered_workqueue("cpt_afpf_mailbox",461WQ_HIGHPRI | WQ_MEM_RECLAIM);462if (!cptpf->afpf_mbox_wq)463return -ENOMEM;464465offset = pci_resource_start(pdev, PCI_MBOX_BAR_NUM);466/* Map AF-PF mailbox memory */467cptpf->afpf_mbox_base = devm_ioremap_wc(&pdev->dev, offset, MBOX_SIZE);468if (!cptpf->afpf_mbox_base) {469dev_err(&pdev->dev, "Unable to map BAR4\n");470err = -ENOMEM;471goto error;472}473474err = otx2_mbox_init(&cptpf->afpf_mbox, cptpf->afpf_mbox_base,475pdev, cptpf->reg_base, MBOX_DIR_PFAF, 1);476if (err)477goto error;478479err = otx2_mbox_init(&cptpf->afpf_mbox_up, cptpf->afpf_mbox_base,480pdev, cptpf->reg_base, MBOX_DIR_PFAF_UP, 1);481if (err)482goto mbox_cleanup;483484INIT_WORK(&cptpf->afpf_mbox_work, otx2_cptpf_afpf_mbox_handler);485INIT_WORK(&cptpf->afpf_mbox_up_work, otx2_cptpf_afpf_mbox_up_handler);486mutex_init(&cptpf->lock);487488return 0;489490mbox_cleanup:491otx2_mbox_destroy(&cptpf->afpf_mbox);492error:493destroy_workqueue(cptpf->afpf_mbox_wq);494return err;495}496497static void cptpf_afpf_mbox_destroy(struct otx2_cptpf_dev *cptpf)498{499destroy_workqueue(cptpf->afpf_mbox_wq);500otx2_mbox_destroy(&cptpf->afpf_mbox);501otx2_mbox_destroy(&cptpf->afpf_mbox_up);502}503504static ssize_t sso_pf_func_ovrd_show(struct device *dev,505struct device_attribute *attr, char *buf)506{507struct otx2_cptpf_dev *cptpf = dev_get_drvdata(dev);508509return sprintf(buf, "%d\n", cptpf->sso_pf_func_ovrd);510}511512static ssize_t sso_pf_func_ovrd_store(struct device *dev,513struct device_attribute *attr,514const char *buf, size_t count)515{516struct otx2_cptpf_dev *cptpf = dev_get_drvdata(dev);517u8 sso_pf_func_ovrd;518519if (!(cptpf->pdev->revision == CPT_UC_RID_CN9K_B0))520return count;521522if (kstrtou8(buf, 0, &sso_pf_func_ovrd))523return -EINVAL;524525cptpf->sso_pf_func_ovrd = sso_pf_func_ovrd;526527return count;528}529530static ssize_t kvf_limits_show(struct device *dev,531struct device_attribute *attr, char *buf)532{533struct otx2_cptpf_dev *cptpf = dev_get_drvdata(dev);534535return sprintf(buf, "%d\n", cptpf->kvf_limits);536}537538static ssize_t kvf_limits_store(struct device *dev,539struct device_attribute *attr,540const char *buf, size_t count)541{542struct otx2_cptpf_dev *cptpf = dev_get_drvdata(dev);543int lfs_num;544int ret;545546ret = kstrtoint(buf, 0, &lfs_num);547if (ret)548return ret;549if (lfs_num < 1 || lfs_num > num_online_cpus()) {550dev_err(dev, "lfs count %d must be in range [1 - %d]\n",551lfs_num, num_online_cpus());552return -EINVAL;553}554cptpf->kvf_limits = lfs_num;555556return count;557}558559static DEVICE_ATTR_RW(kvf_limits);560static DEVICE_ATTR_RW(sso_pf_func_ovrd);561562static struct attribute *cptpf_attrs[] = {563&dev_attr_kvf_limits.attr,564&dev_attr_sso_pf_func_ovrd.attr,565NULL566};567568static const struct attribute_group cptpf_sysfs_group = {569.attrs = cptpf_attrs,570};571572static int cpt_is_pf_usable(struct otx2_cptpf_dev *cptpf)573{574u64 rev;575576rev = otx2_cpt_read64(cptpf->reg_base, BLKADDR_RVUM, 0,577RVU_PF_BLOCK_ADDRX_DISC(BLKADDR_RVUM));578rev = (rev >> 12) & 0xFF;579/*580* Check if AF has setup revision for RVUM block, otherwise581* driver probe should be deferred until AF driver comes up582*/583if (!rev) {584dev_warn(&cptpf->pdev->dev,585"AF is not initialized, deferring probe\n");586return -EPROBE_DEFER;587}588return 0;589}590591static void cptpf_get_rid(struct pci_dev *pdev, struct otx2_cptpf_dev *cptpf)592{593struct otx2_cpt_eng_grps *eng_grps = &cptpf->eng_grps;594u64 reg_val = 0x0;595596if (is_dev_otx2(pdev)) {597eng_grps->rid = pdev->revision;598return;599}600otx2_cpt_read_af_reg(&cptpf->afpf_mbox, pdev, CPT_AF_CTL, ®_val,601BLKADDR_CPT0);602if ((cpt_feature_sgv2(pdev) && (reg_val & BIT_ULL(18))) ||603is_dev_cn10ka_ax(pdev))604eng_grps->rid = CPT_UC_RID_CN10K_A;605else if (cpt_feature_sgv2(pdev))606eng_grps->rid = CPT_UC_RID_CN10K_B;607}608609static void cptpf_check_block_implemented(struct otx2_cptpf_dev *cptpf)610{611u64 cfg;612613cfg = otx2_cpt_read64(cptpf->reg_base, BLKADDR_RVUM, 0,614RVU_PF_BLOCK_ADDRX_DISC(BLKADDR_CPT1));615if (cfg & BIT_ULL(11))616cptpf->has_cpt1 = true;617}618619static int cptpf_device_init(struct otx2_cptpf_dev *cptpf)620{621union otx2_cptx_af_constants1 af_cnsts1 = {0};622int ret = 0;623624/* check if 'implemented' bit is set for block BLKADDR_CPT1 */625cptpf_check_block_implemented(cptpf);626627/* Get number of SE, IE and AE engines */628ret = otx2_cpt_read_af_reg(&cptpf->afpf_mbox, cptpf->pdev,629CPT_AF_CONSTANTS1, &af_cnsts1.u,630BLKADDR_CPT0);631if (ret)632return ret;633634cptpf->eng_grps.avail.max_se_cnt = af_cnsts1.s.se;635cptpf->eng_grps.avail.max_ie_cnt = af_cnsts1.s.ie;636cptpf->eng_grps.avail.max_ae_cnt = af_cnsts1.s.ae;637638/* Disable all cores */639ret = otx2_cpt_disable_all_cores(cptpf);640641otx2_cptlf_set_dev_info(&cptpf->lfs, cptpf->pdev, cptpf->reg_base,642&cptpf->afpf_mbox, BLKADDR_CPT0);643if (cptpf->has_cpt1)644otx2_cptlf_set_dev_info(&cptpf->cpt1_lfs, cptpf->pdev,645cptpf->reg_base, &cptpf->afpf_mbox,646BLKADDR_CPT1);647return ret;648}649650static int cptpf_sriov_disable(struct pci_dev *pdev)651{652struct otx2_cptpf_dev *cptpf = pci_get_drvdata(pdev);653int num_vfs = pci_num_vf(pdev);654655if (!num_vfs)656return 0;657658pci_disable_sriov(pdev);659cptpf_unregister_vfpf_intr(cptpf, num_vfs);660cptpf_flr_wq_destroy(cptpf);661cptpf_vfpf_mbox_destroy(cptpf);662module_put(THIS_MODULE);663cptpf->enabled_vfs = 0;664665return 0;666}667668static int cptpf_sriov_enable(struct pci_dev *pdev, int num_vfs)669{670struct otx2_cptpf_dev *cptpf = pci_get_drvdata(pdev);671int ret;672673/* Initialize VF<=>PF mailbox */674ret = cptpf_vfpf_mbox_init(cptpf, num_vfs);675if (ret)676return ret;677678ret = cptpf_flr_wq_init(cptpf, num_vfs);679if (ret)680goto destroy_mbox;681/* Register VF<=>PF mailbox interrupt */682ret = cptpf_register_vfpf_intr(cptpf, num_vfs);683if (ret)684goto destroy_flr;685686cptpf_get_rid(pdev, cptpf);687/* Get CPT HW capabilities using LOAD_FVC operation. */688ret = otx2_cpt_discover_eng_capabilities(cptpf);689if (ret)690goto disable_intr;691692ret = otx2_cpt_create_eng_grps(cptpf, &cptpf->eng_grps);693if (ret)694goto disable_intr;695696cptpf->enabled_vfs = num_vfs;697ret = pci_enable_sriov(pdev, num_vfs);698if (ret)699goto disable_intr;700701dev_notice(&cptpf->pdev->dev, "VFs enabled: %d\n", num_vfs);702703try_module_get(THIS_MODULE);704return num_vfs;705706disable_intr:707cptpf_unregister_vfpf_intr(cptpf, num_vfs);708cptpf->enabled_vfs = 0;709destroy_flr:710cptpf_flr_wq_destroy(cptpf);711destroy_mbox:712cptpf_vfpf_mbox_destroy(cptpf);713return ret;714}715716static int otx2_cptpf_sriov_configure(struct pci_dev *pdev, int num_vfs)717{718if (num_vfs > 0) {719return cptpf_sriov_enable(pdev, num_vfs);720} else {721return cptpf_sriov_disable(pdev);722}723}724725static int otx2_cptpf_probe(struct pci_dev *pdev,726const struct pci_device_id *ent)727{728struct device *dev = &pdev->dev;729struct otx2_cptpf_dev *cptpf;730int err, num_vec;731732cptpf = devm_kzalloc(dev, sizeof(*cptpf), GFP_KERNEL);733if (!cptpf)734return -ENOMEM;735736err = pcim_enable_device(pdev);737if (err) {738dev_err(dev, "Failed to enable PCI device\n");739goto clear_drvdata;740}741742err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));743if (err) {744dev_err(dev, "Unable to get usable DMA configuration\n");745goto clear_drvdata;746}747err = pcim_request_all_regions(pdev, OTX2_CPT_DRV_NAME);748if (err) {749dev_err(dev, "Couldn't request PCI resources 0x%x\n", err);750goto clear_drvdata;751}752pci_set_master(pdev);753pci_set_drvdata(pdev, cptpf);754cptpf->pdev = pdev;755756/* Map PF's configuration registers */757cptpf->reg_base = pcim_iomap(pdev, PCI_PF_REG_BAR_NUM, 0);758if (!cptpf->reg_base) {759err = -ENOMEM;760dev_err(dev, "Couldn't ioremap PCI resource 0x%x\n", err);761goto clear_drvdata;762}763764/* Check if AF driver is up, otherwise defer probe */765err = cpt_is_pf_usable(cptpf);766if (err)767goto clear_drvdata;768769num_vec = pci_msix_vec_count(cptpf->pdev);770if (num_vec <= 0) {771err = -EINVAL;772goto clear_drvdata;773}774775err = pci_alloc_irq_vectors(pdev, num_vec, num_vec, PCI_IRQ_MSIX);776if (err < 0) {777dev_err(dev, "Request for %d msix vectors failed\n",778RVU_PF_INT_VEC_CNT);779goto clear_drvdata;780}781otx2_cpt_set_hw_caps(pdev, &cptpf->cap_flag);782/* Initialize AF-PF mailbox */783err = cptpf_afpf_mbox_init(cptpf);784if (err)785goto clear_drvdata;786/* Register mailbox interrupt */787err = cptpf_register_afpf_mbox_intr(cptpf);788if (err)789goto destroy_afpf_mbox;790791cptpf->max_vfs = pci_sriov_get_totalvfs(pdev);792cptpf->kvf_limits = 1;793794/* Initialize CPT PF device */795err = cptpf_device_init(cptpf);796if (err)797goto unregister_intr;798799err = cn10k_cptpf_lmtst_init(cptpf);800if (err)801goto unregister_intr;802803/* Initialize engine groups */804err = otx2_cpt_init_eng_grps(pdev, &cptpf->eng_grps);805if (err)806goto free_lmtst;807808err = sysfs_create_group(&dev->kobj, &cptpf_sysfs_group);809if (err)810goto cleanup_eng_grps;811812err = otx2_cpt_register_dl(cptpf);813if (err)814goto sysfs_grp_del;815816return 0;817818sysfs_grp_del:819sysfs_remove_group(&dev->kobj, &cptpf_sysfs_group);820cleanup_eng_grps:821otx2_cpt_cleanup_eng_grps(pdev, &cptpf->eng_grps);822free_lmtst:823cn10k_cpt_lmtst_free(pdev, &cptpf->lfs);824unregister_intr:825cptpf_disable_afpf_mbox_intr(cptpf);826destroy_afpf_mbox:827cptpf_afpf_mbox_destroy(cptpf);828clear_drvdata:829pci_set_drvdata(pdev, NULL);830return err;831}832833static void otx2_cptpf_remove(struct pci_dev *pdev)834{835struct otx2_cptpf_dev *cptpf = pci_get_drvdata(pdev);836837if (!cptpf)838return;839840cptpf_sriov_disable(pdev);841otx2_cpt_unregister_dl(cptpf);842843/* Cleanup Inline CPT LF's if attached */844if (cptpf->lfs.lfs_num)845otx2_inline_cptlf_cleanup(&cptpf->lfs);846847if (cptpf->cpt1_lfs.lfs_num)848otx2_inline_cptlf_cleanup(&cptpf->cpt1_lfs);849850/* Delete sysfs entry created for kernel VF limits */851sysfs_remove_group(&pdev->dev.kobj, &cptpf_sysfs_group);852/* Cleanup engine groups */853otx2_cpt_cleanup_eng_grps(pdev, &cptpf->eng_grps);854/* Disable AF-PF mailbox interrupt */855cptpf_disable_afpf_mbox_intr(cptpf);856/* Destroy AF-PF mbox */857cptpf_afpf_mbox_destroy(cptpf);858/* Free LMTST memory */859cn10k_cpt_lmtst_free(pdev, &cptpf->lfs);860pci_set_drvdata(pdev, NULL);861}862863/* Supported devices */864static const struct pci_device_id otx2_cpt_id_table[] = {865{ PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, OTX2_CPT_PCI_PF_DEVICE_ID) },866{ PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, CN10K_CPT_PCI_PF_DEVICE_ID) },867{ 0, } /* end of table */868};869870static struct pci_driver otx2_cpt_pci_driver = {871.name = OTX2_CPT_DRV_NAME,872.id_table = otx2_cpt_id_table,873.probe = otx2_cptpf_probe,874.remove = otx2_cptpf_remove,875.sriov_configure = otx2_cptpf_sriov_configure876};877878module_pci_driver(otx2_cpt_pci_driver);879880MODULE_IMPORT_NS("CRYPTO_DEV_OCTEONTX2_CPT");881882MODULE_AUTHOR("Marvell");883MODULE_DESCRIPTION(OTX2_CPT_DRV_STRING);884MODULE_LICENSE("GPL v2");885MODULE_DEVICE_TABLE(pci, otx2_cpt_id_table);886887888