Path: blob/master/drivers/crypto/marvell/octeontx2/otx2_cptpf_main.c
53997 views
// SPDX-License-Identifier: GPL-2.0-only1/* Copyright (C) 2020 Marvell. */23#include <linux/firmware.h>4#include <linux/sysfs.h>5#include "otx2_cpt_hw_types.h"6#include "otx2_cpt_common.h"7#include "otx2_cpt_devlink.h"8#include "otx2_cptpf_ucode.h"9#include "otx2_cptpf.h"10#include "cn10k_cpt.h"11#include "rvu_reg.h"1213#define OTX2_CPT_DRV_NAME "rvu_cptpf"14#define OTX2_CPT_DRV_STRING "Marvell RVU CPT Physical Function Driver"1516#define CPT_UC_RID_CN9K_B0 117#define CPT_UC_RID_CN10K_A 418#define CPT_UC_RID_CN10K_B 51920static void cptpf_enable_vfpf_mbox_intr(struct otx2_cptpf_dev *cptpf,21int num_vfs)22{23int ena_bits;2425/* Clear any pending interrupts */26otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,27RVU_PF_VFPF_MBOX_INTX(0), ~0x0ULL);28otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,29RVU_PF_VFPF_MBOX_INTX(1), ~0x0ULL);3031/* Enable VF interrupts for VFs from 0 to 63 */32ena_bits = ((num_vfs - 1) % 64);33otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,34RVU_PF_VFPF_MBOX_INT_ENA_W1SX(0),35GENMASK_ULL(ena_bits, 0));3637if (num_vfs > 64) {38/* Enable VF interrupts for VFs from 64 to 127 */39ena_bits = num_vfs - 64 - 1;40otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,41RVU_PF_VFPF_MBOX_INT_ENA_W1SX(1),42GENMASK_ULL(ena_bits, 0));43}44}4546static void cptpf_disable_vfpf_mbox_intr(struct otx2_cptpf_dev *cptpf,47int num_vfs)48{49int vector;5051/* Disable VF-PF interrupts */52otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,53RVU_PF_VFPF_MBOX_INT_ENA_W1CX(0), ~0ULL);54otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,55RVU_PF_VFPF_MBOX_INT_ENA_W1CX(1), ~0ULL);56/* Clear any pending interrupts */57otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,58RVU_PF_VFPF_MBOX_INTX(0), ~0ULL);5960vector = pci_irq_vector(cptpf->pdev, RVU_PF_INT_VEC_VFPF_MBOX0);61free_irq(vector, cptpf);6263if (num_vfs > 64) {64otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,65RVU_PF_VFPF_MBOX_INTX(1), ~0ULL);66vector = pci_irq_vector(cptpf->pdev, RVU_PF_INT_VEC_VFPF_MBOX1);67free_irq(vector, cptpf);68}69}7071static void cptpf_enable_vf_flr_me_intrs(struct otx2_cptpf_dev *cptpf,72int num_vfs)73{74/* Clear FLR interrupt if any */75otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0, RVU_PF_VFFLR_INTX(0),76INTR_MASK(num_vfs));7778/* Enable VF FLR interrupts */79otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,80RVU_PF_VFFLR_INT_ENA_W1SX(0), INTR_MASK(num_vfs));81/* Clear ME interrupt if any */82otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0, RVU_PF_VFME_INTX(0),83INTR_MASK(num_vfs));84/* Enable VF ME interrupts */85otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,86RVU_PF_VFME_INT_ENA_W1SX(0), INTR_MASK(num_vfs));8788if (num_vfs <= 64)89return;9091otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0, RVU_PF_VFFLR_INTX(1),92INTR_MASK(num_vfs - 64));93otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,94RVU_PF_VFFLR_INT_ENA_W1SX(1), INTR_MASK(num_vfs - 64));9596otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0, RVU_PF_VFME_INTX(1),97INTR_MASK(num_vfs - 64));98otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,99RVU_PF_VFME_INT_ENA_W1SX(1), INTR_MASK(num_vfs - 64));100}101102static void cptpf_disable_vf_flr_me_intrs(struct otx2_cptpf_dev *cptpf,103int num_vfs)104{105int vector;106107/* Disable VF FLR interrupts */108otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,109RVU_PF_VFFLR_INT_ENA_W1CX(0), INTR_MASK(num_vfs));110vector = pci_irq_vector(cptpf->pdev, RVU_PF_INT_VEC_VFFLR0);111free_irq(vector, cptpf);112113/* Disable VF ME interrupts */114otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,115RVU_PF_VFME_INT_ENA_W1CX(0), INTR_MASK(num_vfs));116vector = pci_irq_vector(cptpf->pdev, RVU_PF_INT_VEC_VFME0);117free_irq(vector, cptpf);118119if (num_vfs <= 64)120return;121122otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,123RVU_PF_VFFLR_INT_ENA_W1CX(1), INTR_MASK(num_vfs - 64));124vector = pci_irq_vector(cptpf->pdev, RVU_PF_INT_VEC_VFFLR1);125free_irq(vector, cptpf);126127otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,128RVU_PF_VFME_INT_ENA_W1CX(1), INTR_MASK(num_vfs - 64));129vector = pci_irq_vector(cptpf->pdev, RVU_PF_INT_VEC_VFME1);130free_irq(vector, cptpf);131}132133static void cptpf_flr_wq_handler(struct work_struct *work)134{135struct cptpf_flr_work *flr_work;136struct otx2_cptpf_dev *pf;137struct mbox_msghdr *req;138struct otx2_mbox *mbox;139int vf, reg = 0;140141flr_work = container_of(work, struct cptpf_flr_work, work);142pf = flr_work->pf;143mbox = &pf->afpf_mbox;144145vf = flr_work - pf->flr_work;146147mutex_lock(&pf->lock);148req = otx2_mbox_alloc_msg_rsp(mbox, 0, sizeof(*req),149sizeof(struct msg_rsp));150if (!req) {151mutex_unlock(&pf->lock);152return;153}154155req->sig = OTX2_MBOX_REQ_SIG;156req->id = MBOX_MSG_VF_FLR;157req->pcifunc &= RVU_PFVF_FUNC_MASK;158req->pcifunc |= (vf + 1) & RVU_PFVF_FUNC_MASK;159160otx2_cpt_send_mbox_msg(mbox, pf->pdev);161if (!otx2_cpt_sync_mbox_msg(&pf->afpf_mbox)) {162163if (vf >= 64) {164reg = 1;165vf = vf - 64;166}167/* Clear transaction pending register */168otx2_cpt_write64(pf->reg_base, BLKADDR_RVUM, 0,169RVU_PF_VFTRPENDX(reg), BIT_ULL(vf));170otx2_cpt_write64(pf->reg_base, BLKADDR_RVUM, 0,171RVU_PF_VFFLR_INT_ENA_W1SX(reg), BIT_ULL(vf));172}173mutex_unlock(&pf->lock);174}175176static irqreturn_t cptpf_vf_flr_intr(int __always_unused irq, void *arg)177{178int reg, dev, vf, start_vf, num_reg = 1;179struct otx2_cptpf_dev *cptpf = arg;180u64 intr;181182if (cptpf->max_vfs > 64)183num_reg = 2;184185for (reg = 0; reg < num_reg; reg++) {186intr = otx2_cpt_read64(cptpf->reg_base, BLKADDR_RVUM, 0,187RVU_PF_VFFLR_INTX(reg));188if (!intr)189continue;190start_vf = 64 * reg;191for (vf = 0; vf < 64; vf++) {192if (!(intr & BIT_ULL(vf)))193continue;194dev = vf + start_vf;195queue_work(cptpf->flr_wq, &cptpf->flr_work[dev].work);196/* Clear interrupt */197otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,198RVU_PF_VFFLR_INTX(reg), BIT_ULL(vf));199/* Disable the interrupt */200otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,201RVU_PF_VFFLR_INT_ENA_W1CX(reg),202BIT_ULL(vf));203}204}205return IRQ_HANDLED;206}207208static irqreturn_t cptpf_vf_me_intr(int __always_unused irq, void *arg)209{210struct otx2_cptpf_dev *cptpf = arg;211int reg, vf, num_reg = 1;212u64 intr;213214if (cptpf->max_vfs > 64)215num_reg = 2;216217for (reg = 0; reg < num_reg; reg++) {218intr = otx2_cpt_read64(cptpf->reg_base, BLKADDR_RVUM, 0,219RVU_PF_VFME_INTX(reg));220if (!intr)221continue;222for (vf = 0; vf < 64; vf++) {223if (!(intr & BIT_ULL(vf)))224continue;225otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,226RVU_PF_VFTRPENDX(reg), BIT_ULL(vf));227/* Clear interrupt */228otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0,229RVU_PF_VFME_INTX(reg), BIT_ULL(vf));230}231}232return IRQ_HANDLED;233}234235static void cptpf_unregister_vfpf_intr(struct otx2_cptpf_dev *cptpf,236int num_vfs)237{238cptpf_disable_vfpf_mbox_intr(cptpf, num_vfs);239cptpf_disable_vf_flr_me_intrs(cptpf, num_vfs);240}241242static int cptpf_register_vfpf_intr(struct otx2_cptpf_dev *cptpf, int num_vfs)243{244struct pci_dev *pdev = cptpf->pdev;245struct device *dev = &pdev->dev;246int ret, vector;247248vector = pci_irq_vector(pdev, RVU_PF_INT_VEC_VFPF_MBOX0);249/* Register VF-PF mailbox interrupt handler */250ret = request_irq(vector, otx2_cptpf_vfpf_mbox_intr, 0, "CPTVFPF Mbox0",251cptpf);252if (ret) {253dev_err(dev,254"IRQ registration failed for PFVF mbox0 irq\n");255return ret;256}257vector = pci_irq_vector(pdev, RVU_PF_INT_VEC_VFFLR0);258/* Register VF FLR interrupt handler */259ret = request_irq(vector, cptpf_vf_flr_intr, 0, "CPTPF FLR0", cptpf);260if (ret) {261dev_err(dev,262"IRQ registration failed for VFFLR0 irq\n");263goto free_mbox0_irq;264}265vector = pci_irq_vector(pdev, RVU_PF_INT_VEC_VFME0);266/* Register VF ME interrupt handler */267ret = request_irq(vector, cptpf_vf_me_intr, 0, "CPTPF ME0", cptpf);268if (ret) {269dev_err(dev,270"IRQ registration failed for PFVF mbox0 irq\n");271goto free_flr0_irq;272}273274if (num_vfs > 64) {275vector = pci_irq_vector(pdev, RVU_PF_INT_VEC_VFPF_MBOX1);276ret = request_irq(vector, otx2_cptpf_vfpf_mbox_intr, 0,277"CPTVFPF Mbox1", cptpf);278if (ret) {279dev_err(dev,280"IRQ registration failed for PFVF mbox1 irq\n");281goto free_me0_irq;282}283vector = pci_irq_vector(pdev, RVU_PF_INT_VEC_VFFLR1);284/* Register VF FLR interrupt handler */285ret = request_irq(vector, cptpf_vf_flr_intr, 0, "CPTPF FLR1",286cptpf);287if (ret) {288dev_err(dev,289"IRQ registration failed for VFFLR1 irq\n");290goto free_mbox1_irq;291}292vector = pci_irq_vector(pdev, RVU_PF_INT_VEC_VFME1);293/* Register VF FLR interrupt handler */294ret = request_irq(vector, cptpf_vf_me_intr, 0, "CPTPF ME1",295cptpf);296if (ret) {297dev_err(dev,298"IRQ registration failed for VFFLR1 irq\n");299goto free_flr1_irq;300}301}302cptpf_enable_vfpf_mbox_intr(cptpf, num_vfs);303cptpf_enable_vf_flr_me_intrs(cptpf, num_vfs);304305return 0;306307free_flr1_irq:308vector = pci_irq_vector(pdev, RVU_PF_INT_VEC_VFFLR1);309free_irq(vector, cptpf);310free_mbox1_irq:311vector = pci_irq_vector(pdev, RVU_PF_INT_VEC_VFPF_MBOX1);312free_irq(vector, cptpf);313free_me0_irq:314vector = pci_irq_vector(pdev, RVU_PF_INT_VEC_VFME0);315free_irq(vector, cptpf);316free_flr0_irq:317vector = pci_irq_vector(pdev, RVU_PF_INT_VEC_VFFLR0);318free_irq(vector, cptpf);319free_mbox0_irq:320vector = pci_irq_vector(pdev, RVU_PF_INT_VEC_VFPF_MBOX0);321free_irq(vector, cptpf);322return ret;323}324325static void cptpf_flr_wq_destroy(struct otx2_cptpf_dev *pf)326{327if (!pf->flr_wq)328return;329destroy_workqueue(pf->flr_wq);330pf->flr_wq = NULL;331kfree(pf->flr_work);332}333334static int cptpf_flr_wq_init(struct otx2_cptpf_dev *cptpf, int num_vfs)335{336int vf;337338cptpf->flr_wq = alloc_ordered_workqueue("cptpf_flr_wq", 0);339if (!cptpf->flr_wq)340return -ENOMEM;341342cptpf->flr_work = kcalloc(num_vfs, sizeof(struct cptpf_flr_work),343GFP_KERNEL);344if (!cptpf->flr_work)345goto destroy_wq;346347for (vf = 0; vf < num_vfs; vf++) {348cptpf->flr_work[vf].pf = cptpf;349INIT_WORK(&cptpf->flr_work[vf].work, cptpf_flr_wq_handler);350}351return 0;352353destroy_wq:354destroy_workqueue(cptpf->flr_wq);355return -ENOMEM;356}357358static int cptpf_vfpf_mbox_init(struct otx2_cptpf_dev *cptpf, int num_vfs)359{360struct device *dev = &cptpf->pdev->dev;361u64 vfpf_mbox_base;362int err, i;363364cptpf->vfpf_mbox_wq =365alloc_ordered_workqueue("cpt_vfpf_mailbox",366WQ_HIGHPRI | WQ_MEM_RECLAIM);367if (!cptpf->vfpf_mbox_wq)368return -ENOMEM;369370/* Map VF-PF mailbox memory */371if (test_bit(CN10K_MBOX, &cptpf->cap_flag))372vfpf_mbox_base = readq(cptpf->reg_base + RVU_PF_VF_MBOX_ADDR);373else374vfpf_mbox_base = readq(cptpf->reg_base + RVU_PF_VF_BAR4_ADDR);375376if (!vfpf_mbox_base) {377dev_err(dev, "VF-PF mailbox address not configured\n");378err = -ENOMEM;379goto free_wqe;380}381cptpf->vfpf_mbox_base = devm_ioremap_wc(dev, vfpf_mbox_base,382MBOX_SIZE * cptpf->max_vfs);383if (!cptpf->vfpf_mbox_base) {384dev_err(dev, "Mapping of VF-PF mailbox address failed\n");385err = -ENOMEM;386goto free_wqe;387}388err = otx2_mbox_init(&cptpf->vfpf_mbox, cptpf->vfpf_mbox_base,389cptpf->pdev, cptpf->reg_base, MBOX_DIR_PFVF,390num_vfs);391if (err)392goto free_wqe;393394for (i = 0; i < num_vfs; i++) {395cptpf->vf[i].vf_id = i;396cptpf->vf[i].cptpf = cptpf;397cptpf->vf[i].intr_idx = i % 64;398INIT_WORK(&cptpf->vf[i].vfpf_mbox_work,399otx2_cptpf_vfpf_mbox_handler);400}401return 0;402403free_wqe:404destroy_workqueue(cptpf->vfpf_mbox_wq);405return err;406}407408static void cptpf_vfpf_mbox_destroy(struct otx2_cptpf_dev *cptpf)409{410destroy_workqueue(cptpf->vfpf_mbox_wq);411otx2_mbox_destroy(&cptpf->vfpf_mbox);412}413414static void cptpf_disable_afpf_mbox_intr(struct otx2_cptpf_dev *cptpf)415{416/* Disable AF-PF interrupt */417otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0, RVU_PF_INT_ENA_W1C,4180x1ULL);419/* Clear interrupt if any */420otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0, RVU_PF_INT, 0x1ULL);421}422423static int cptpf_register_afpf_mbox_intr(struct otx2_cptpf_dev *cptpf)424{425struct pci_dev *pdev = cptpf->pdev;426struct device *dev = &pdev->dev;427int ret, irq;428429irq = pci_irq_vector(pdev, RVU_PF_INT_VEC_AFPF_MBOX);430/* Register AF-PF mailbox interrupt handler */431ret = devm_request_irq(dev, irq, otx2_cptpf_afpf_mbox_intr, 0,432"CPTAFPF Mbox", cptpf);433if (ret) {434dev_err(dev,435"IRQ registration failed for PFAF mbox irq\n");436return ret;437}438/* Clear interrupt if any, to avoid spurious interrupts */439otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0, RVU_PF_INT, 0x1ULL);440/* Enable AF-PF interrupt */441otx2_cpt_write64(cptpf->reg_base, BLKADDR_RVUM, 0, RVU_PF_INT_ENA_W1S,4420x1ULL);443444ret = otx2_cpt_send_ready_msg(&cptpf->afpf_mbox, cptpf->pdev);445if (ret) {446dev_warn(dev,447"AF not responding to mailbox, deferring probe\n");448cptpf_disable_afpf_mbox_intr(cptpf);449return -EPROBE_DEFER;450}451return 0;452}453454static int cptpf_afpf_mbox_init(struct otx2_cptpf_dev *cptpf)455{456struct pci_dev *pdev = cptpf->pdev;457resource_size_t offset;458int err;459460cptpf->afpf_mbox_wq =461alloc_ordered_workqueue("cpt_afpf_mailbox",462WQ_HIGHPRI | WQ_MEM_RECLAIM);463if (!cptpf->afpf_mbox_wq)464return -ENOMEM;465466offset = pci_resource_start(pdev, PCI_MBOX_BAR_NUM);467/* Map AF-PF mailbox memory */468cptpf->afpf_mbox_base = devm_ioremap_wc(&pdev->dev, offset, MBOX_SIZE);469if (!cptpf->afpf_mbox_base) {470dev_err(&pdev->dev, "Unable to map BAR4\n");471err = -ENOMEM;472goto error;473}474475err = otx2_mbox_init(&cptpf->afpf_mbox, cptpf->afpf_mbox_base,476pdev, cptpf->reg_base, MBOX_DIR_PFAF, 1);477if (err)478goto error;479480err = otx2_mbox_init(&cptpf->afpf_mbox_up, cptpf->afpf_mbox_base,481pdev, cptpf->reg_base, MBOX_DIR_PFAF_UP, 1);482if (err)483goto mbox_cleanup;484485INIT_WORK(&cptpf->afpf_mbox_work, otx2_cptpf_afpf_mbox_handler);486INIT_WORK(&cptpf->afpf_mbox_up_work, otx2_cptpf_afpf_mbox_up_handler);487mutex_init(&cptpf->lock);488489return 0;490491mbox_cleanup:492otx2_mbox_destroy(&cptpf->afpf_mbox);493error:494destroy_workqueue(cptpf->afpf_mbox_wq);495return err;496}497498static void cptpf_afpf_mbox_destroy(struct otx2_cptpf_dev *cptpf)499{500destroy_workqueue(cptpf->afpf_mbox_wq);501otx2_mbox_destroy(&cptpf->afpf_mbox);502otx2_mbox_destroy(&cptpf->afpf_mbox_up);503}504505static ssize_t sso_pf_func_ovrd_show(struct device *dev,506struct device_attribute *attr, char *buf)507{508struct otx2_cptpf_dev *cptpf = dev_get_drvdata(dev);509510return sysfs_emit(buf, "%d\n", cptpf->sso_pf_func_ovrd);511}512513static ssize_t sso_pf_func_ovrd_store(struct device *dev,514struct device_attribute *attr,515const char *buf, size_t count)516{517struct otx2_cptpf_dev *cptpf = dev_get_drvdata(dev);518u8 sso_pf_func_ovrd;519520if (!(cptpf->pdev->revision == CPT_UC_RID_CN9K_B0))521return count;522523if (kstrtou8(buf, 0, &sso_pf_func_ovrd))524return -EINVAL;525526cptpf->sso_pf_func_ovrd = sso_pf_func_ovrd;527528return count;529}530531static ssize_t kvf_limits_show(struct device *dev,532struct device_attribute *attr, char *buf)533{534struct otx2_cptpf_dev *cptpf = dev_get_drvdata(dev);535536return sysfs_emit(buf, "%d\n", cptpf->kvf_limits);537}538539static ssize_t kvf_limits_store(struct device *dev,540struct device_attribute *attr,541const char *buf, size_t count)542{543struct otx2_cptpf_dev *cptpf = dev_get_drvdata(dev);544int lfs_num;545int ret;546547ret = kstrtoint(buf, 0, &lfs_num);548if (ret)549return ret;550if (lfs_num < 1 || lfs_num > num_online_cpus()) {551dev_err(dev, "lfs count %d must be in range [1 - %d]\n",552lfs_num, num_online_cpus());553return -EINVAL;554}555cptpf->kvf_limits = lfs_num;556557return count;558}559560static DEVICE_ATTR_RW(kvf_limits);561static DEVICE_ATTR_RW(sso_pf_func_ovrd);562563static struct attribute *cptpf_attrs[] = {564&dev_attr_kvf_limits.attr,565&dev_attr_sso_pf_func_ovrd.attr,566NULL567};568569static const struct attribute_group cptpf_sysfs_group = {570.attrs = cptpf_attrs,571};572573static int cpt_is_pf_usable(struct otx2_cptpf_dev *cptpf)574{575u64 rev;576577rev = otx2_cpt_read64(cptpf->reg_base, BLKADDR_RVUM, 0,578RVU_PF_BLOCK_ADDRX_DISC(BLKADDR_RVUM));579rev = (rev >> 12) & 0xFF;580/*581* Check if AF has setup revision for RVUM block, otherwise582* driver probe should be deferred until AF driver comes up583*/584if (!rev) {585dev_warn(&cptpf->pdev->dev,586"AF is not initialized, deferring probe\n");587return -EPROBE_DEFER;588}589return 0;590}591592static void cptpf_get_rid(struct pci_dev *pdev, struct otx2_cptpf_dev *cptpf)593{594struct otx2_cpt_eng_grps *eng_grps = &cptpf->eng_grps;595u64 reg_val = 0x0;596597if (is_dev_otx2(pdev)) {598eng_grps->rid = pdev->revision;599return;600}601otx2_cpt_read_af_reg(&cptpf->afpf_mbox, pdev, CPT_AF_CTL, ®_val,602BLKADDR_CPT0);603if ((cpt_feature_sgv2(pdev) && (reg_val & BIT_ULL(18))) ||604is_dev_cn10ka_ax(pdev))605eng_grps->rid = CPT_UC_RID_CN10K_A;606else if (cpt_feature_sgv2(pdev))607eng_grps->rid = CPT_UC_RID_CN10K_B;608}609610static void cptpf_check_block_implemented(struct otx2_cptpf_dev *cptpf)611{612u64 cfg;613614cfg = otx2_cpt_read64(cptpf->reg_base, BLKADDR_RVUM, 0,615RVU_PF_BLOCK_ADDRX_DISC(BLKADDR_CPT1));616if (cfg & BIT_ULL(11))617cptpf->has_cpt1 = true;618}619620static int cptpf_device_init(struct otx2_cptpf_dev *cptpf)621{622union otx2_cptx_af_constants1 af_cnsts1 = {0};623int ret = 0;624625/* check if 'implemented' bit is set for block BLKADDR_CPT1 */626cptpf_check_block_implemented(cptpf);627628/* Get number of SE, IE and AE engines */629ret = otx2_cpt_read_af_reg(&cptpf->afpf_mbox, cptpf->pdev,630CPT_AF_CONSTANTS1, &af_cnsts1.u,631BLKADDR_CPT0);632if (ret)633return ret;634635cptpf->eng_grps.avail.max_se_cnt = af_cnsts1.s.se;636cptpf->eng_grps.avail.max_ie_cnt = af_cnsts1.s.ie;637cptpf->eng_grps.avail.max_ae_cnt = af_cnsts1.s.ae;638639/* Disable all cores */640ret = otx2_cpt_disable_all_cores(cptpf);641642otx2_cptlf_set_dev_info(&cptpf->lfs, cptpf->pdev, cptpf->reg_base,643&cptpf->afpf_mbox, BLKADDR_CPT0);644if (cptpf->has_cpt1)645otx2_cptlf_set_dev_info(&cptpf->cpt1_lfs, cptpf->pdev,646cptpf->reg_base, &cptpf->afpf_mbox,647BLKADDR_CPT1);648return ret;649}650651static int cptpf_sriov_disable(struct pci_dev *pdev)652{653struct otx2_cptpf_dev *cptpf = pci_get_drvdata(pdev);654int num_vfs = pci_num_vf(pdev);655656if (!num_vfs)657return 0;658659pci_disable_sriov(pdev);660cptpf_unregister_vfpf_intr(cptpf, num_vfs);661cptpf_flr_wq_destroy(cptpf);662cptpf_vfpf_mbox_destroy(cptpf);663module_put(THIS_MODULE);664cptpf->enabled_vfs = 0;665666return 0;667}668669static int cptpf_sriov_enable(struct pci_dev *pdev, int num_vfs)670{671struct otx2_cptpf_dev *cptpf = pci_get_drvdata(pdev);672int ret;673674/* Initialize VF<=>PF mailbox */675ret = cptpf_vfpf_mbox_init(cptpf, num_vfs);676if (ret)677return ret;678679ret = cptpf_flr_wq_init(cptpf, num_vfs);680if (ret)681goto destroy_mbox;682/* Register VF<=>PF mailbox interrupt */683ret = cptpf_register_vfpf_intr(cptpf, num_vfs);684if (ret)685goto destroy_flr;686687cptpf_get_rid(pdev, cptpf);688/* Get CPT HW capabilities using LOAD_FVC operation. */689ret = otx2_cpt_discover_eng_capabilities(cptpf);690if (ret)691goto disable_intr;692693ret = otx2_cpt_create_eng_grps(cptpf, &cptpf->eng_grps);694if (ret)695goto disable_intr;696697cptpf->enabled_vfs = num_vfs;698ret = pci_enable_sriov(pdev, num_vfs);699if (ret)700goto disable_intr;701702dev_notice(&cptpf->pdev->dev, "VFs enabled: %d\n", num_vfs);703704try_module_get(THIS_MODULE);705return num_vfs;706707disable_intr:708cptpf_unregister_vfpf_intr(cptpf, num_vfs);709cptpf->enabled_vfs = 0;710destroy_flr:711cptpf_flr_wq_destroy(cptpf);712destroy_mbox:713cptpf_vfpf_mbox_destroy(cptpf);714return ret;715}716717static int otx2_cptpf_sriov_configure(struct pci_dev *pdev, int num_vfs)718{719if (num_vfs > 0) {720return cptpf_sriov_enable(pdev, num_vfs);721} else {722return cptpf_sriov_disable(pdev);723}724}725726static int otx2_cptpf_probe(struct pci_dev *pdev,727const struct pci_device_id *ent)728{729struct device *dev = &pdev->dev;730struct otx2_cptpf_dev *cptpf;731int err, num_vec;732733cptpf = devm_kzalloc(dev, sizeof(*cptpf), GFP_KERNEL);734if (!cptpf)735return -ENOMEM;736737err = pcim_enable_device(pdev);738if (err) {739dev_err(dev, "Failed to enable PCI device\n");740goto clear_drvdata;741}742743err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));744if (err) {745dev_err(dev, "Unable to get usable DMA configuration\n");746goto clear_drvdata;747}748err = pcim_request_all_regions(pdev, OTX2_CPT_DRV_NAME);749if (err) {750dev_err(dev, "Couldn't request PCI resources 0x%x\n", err);751goto clear_drvdata;752}753pci_set_master(pdev);754pci_set_drvdata(pdev, cptpf);755cptpf->pdev = pdev;756757/* Map PF's configuration registers */758cptpf->reg_base = pcim_iomap(pdev, PCI_PF_REG_BAR_NUM, 0);759if (!cptpf->reg_base) {760err = -ENOMEM;761dev_err(dev, "Couldn't ioremap PCI resource 0x%x\n", err);762goto clear_drvdata;763}764765/* Check if AF driver is up, otherwise defer probe */766err = cpt_is_pf_usable(cptpf);767if (err)768goto clear_drvdata;769770num_vec = pci_msix_vec_count(cptpf->pdev);771if (num_vec <= 0) {772err = -EINVAL;773goto clear_drvdata;774}775776err = pci_alloc_irq_vectors(pdev, num_vec, num_vec, PCI_IRQ_MSIX);777if (err < 0) {778dev_err(dev, "Request for %d msix vectors failed\n",779RVU_PF_INT_VEC_CNT);780goto clear_drvdata;781}782otx2_cpt_set_hw_caps(pdev, &cptpf->cap_flag);783/* Initialize AF-PF mailbox */784err = cptpf_afpf_mbox_init(cptpf);785if (err)786goto clear_drvdata;787/* Register mailbox interrupt */788err = cptpf_register_afpf_mbox_intr(cptpf);789if (err)790goto destroy_afpf_mbox;791792cptpf->max_vfs = pci_sriov_get_totalvfs(pdev);793cptpf->kvf_limits = 1;794795/* Initialize CPT PF device */796err = cptpf_device_init(cptpf);797if (err)798goto unregister_intr;799800err = cn10k_cptpf_lmtst_init(cptpf);801if (err)802goto unregister_intr;803804/* Initialize engine groups */805err = otx2_cpt_init_eng_grps(pdev, &cptpf->eng_grps);806if (err)807goto free_lmtst;808809err = sysfs_create_group(&dev->kobj, &cptpf_sysfs_group);810if (err)811goto cleanup_eng_grps;812813err = otx2_cpt_register_dl(cptpf);814if (err)815goto sysfs_grp_del;816817return 0;818819sysfs_grp_del:820sysfs_remove_group(&dev->kobj, &cptpf_sysfs_group);821cleanup_eng_grps:822otx2_cpt_cleanup_eng_grps(pdev, &cptpf->eng_grps);823free_lmtst:824cn10k_cpt_lmtst_free(pdev, &cptpf->lfs);825unregister_intr:826cptpf_disable_afpf_mbox_intr(cptpf);827destroy_afpf_mbox:828cptpf_afpf_mbox_destroy(cptpf);829clear_drvdata:830pci_set_drvdata(pdev, NULL);831return err;832}833834static void otx2_cptpf_remove(struct pci_dev *pdev)835{836struct otx2_cptpf_dev *cptpf = pci_get_drvdata(pdev);837838if (!cptpf)839return;840841cptpf_sriov_disable(pdev);842otx2_cpt_unregister_dl(cptpf);843844/* Cleanup Inline CPT LF's if attached */845if (cptpf->lfs.lfs_num)846otx2_inline_cptlf_cleanup(&cptpf->lfs);847848if (cptpf->cpt1_lfs.lfs_num)849otx2_inline_cptlf_cleanup(&cptpf->cpt1_lfs);850851/* Delete sysfs entry created for kernel VF limits */852sysfs_remove_group(&pdev->dev.kobj, &cptpf_sysfs_group);853/* Cleanup engine groups */854otx2_cpt_cleanup_eng_grps(pdev, &cptpf->eng_grps);855/* Disable AF-PF mailbox interrupt */856cptpf_disable_afpf_mbox_intr(cptpf);857/* Destroy AF-PF mbox */858cptpf_afpf_mbox_destroy(cptpf);859/* Free LMTST memory */860cn10k_cpt_lmtst_free(pdev, &cptpf->lfs);861pci_set_drvdata(pdev, NULL);862}863864/* Supported devices */865static const struct pci_device_id otx2_cpt_id_table[] = {866{ PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, OTX2_CPT_PCI_PF_DEVICE_ID) },867{ PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, CN10K_CPT_PCI_PF_DEVICE_ID) },868{ 0, } /* end of table */869};870871static struct pci_driver otx2_cpt_pci_driver = {872.name = OTX2_CPT_DRV_NAME,873.id_table = otx2_cpt_id_table,874.probe = otx2_cptpf_probe,875.remove = otx2_cptpf_remove,876.sriov_configure = otx2_cptpf_sriov_configure877};878879module_pci_driver(otx2_cpt_pci_driver);880881MODULE_IMPORT_NS("CRYPTO_DEV_OCTEONTX2_CPT");882883MODULE_AUTHOR("Marvell");884MODULE_DESCRIPTION(OTX2_CPT_DRV_STRING);885MODULE_LICENSE("GPL v2");886MODULE_DEVICE_TABLE(pci, otx2_cpt_id_table);887888889