Path: blob/master/drivers/crypto/marvell/octeontx/otx_cptpf_main.c
26285 views
// SPDX-License-Identifier: GPL-2.01/* Marvell OcteonTX CPT driver2*3* Copyright (C) 2019 Marvell International Ltd.4*5* This program is free software; you can redistribute it and/or modify6* it under the terms of the GNU General Public License version 2 as7* published by the Free Software Foundation.8*/910#include "otx_cpt_common.h"11#include "otx_cptpf.h"1213#define DRV_NAME "octeontx-cpt"14#define DRV_VERSION "1.0"1516static void otx_cpt_disable_mbox_interrupts(struct otx_cpt_device *cpt)17{18/* Disable mbox(0) interrupts for all VFs */19writeq(~0ull, cpt->reg_base + OTX_CPT_PF_MBOX_ENA_W1CX(0));20}2122static void otx_cpt_enable_mbox_interrupts(struct otx_cpt_device *cpt)23{24/* Enable mbox(0) interrupts for all VFs */25writeq(~0ull, cpt->reg_base + OTX_CPT_PF_MBOX_ENA_W1SX(0));26}2728static irqreturn_t otx_cpt_mbx0_intr_handler(int __always_unused irq,29void *cpt)30{31otx_cpt_mbox_intr_handler(cpt, 0);3233return IRQ_HANDLED;34}3536static void otx_cpt_reset(struct otx_cpt_device *cpt)37{38writeq(1, cpt->reg_base + OTX_CPT_PF_RESET);39}4041static void otx_cpt_find_max_enabled_cores(struct otx_cpt_device *cpt)42{43union otx_cptx_pf_constants pf_cnsts = {0};4445pf_cnsts.u = readq(cpt->reg_base + OTX_CPT_PF_CONSTANTS);46cpt->eng_grps.avail.max_se_cnt = pf_cnsts.s.se;47cpt->eng_grps.avail.max_ae_cnt = pf_cnsts.s.ae;48}4950static u32 otx_cpt_check_bist_status(struct otx_cpt_device *cpt)51{52union otx_cptx_pf_bist_status bist_sts = {0};5354bist_sts.u = readq(cpt->reg_base + OTX_CPT_PF_BIST_STATUS);55return bist_sts.u;56}5758static u64 otx_cpt_check_exe_bist_status(struct otx_cpt_device *cpt)59{60union otx_cptx_pf_exe_bist_status bist_sts = {0};6162bist_sts.u = readq(cpt->reg_base + OTX_CPT_PF_EXE_BIST_STATUS);63return bist_sts.u;64}6566static int otx_cpt_device_init(struct otx_cpt_device *cpt)67{68struct device *dev = &cpt->pdev->dev;69u16 sdevid;70u64 bist;7172/* Reset the PF when probed first */73otx_cpt_reset(cpt);74mdelay(100);7576pci_read_config_word(cpt->pdev, PCI_SUBSYSTEM_ID, &sdevid);7778/* Check BIST status */79bist = (u64)otx_cpt_check_bist_status(cpt);80if (bist) {81dev_err(dev, "RAM BIST failed with code 0x%llx\n", bist);82return -ENODEV;83}8485bist = otx_cpt_check_exe_bist_status(cpt);86if (bist) {87dev_err(dev, "Engine BIST failed with code 0x%llx\n", bist);88return -ENODEV;89}9091/* Get max enabled cores */92otx_cpt_find_max_enabled_cores(cpt);9394if ((sdevid == OTX_CPT_PCI_PF_SUBSYS_ID) &&95(cpt->eng_grps.avail.max_se_cnt == 0)) {96cpt->pf_type = OTX_CPT_AE;97} else if ((sdevid == OTX_CPT_PCI_PF_SUBSYS_ID) &&98(cpt->eng_grps.avail.max_ae_cnt == 0)) {99cpt->pf_type = OTX_CPT_SE;100}101102/* Get max VQs/VFs supported by the device */103cpt->max_vfs = pci_sriov_get_totalvfs(cpt->pdev);104105/* Disable all cores */106otx_cpt_disable_all_cores(cpt);107108return 0;109}110111static int otx_cpt_register_interrupts(struct otx_cpt_device *cpt)112{113struct device *dev = &cpt->pdev->dev;114u32 mbox_int_idx = OTX_CPT_PF_MBOX_INT;115u32 num_vec = OTX_CPT_PF_MSIX_VECTORS;116int ret;117118/* Enable MSI-X */119ret = pci_alloc_irq_vectors(cpt->pdev, num_vec, num_vec, PCI_IRQ_MSIX);120if (ret < 0) {121dev_err(&cpt->pdev->dev,122"Request for #%d msix vectors failed\n",123num_vec);124return ret;125}126127/* Register mailbox interrupt handlers */128ret = request_irq(pci_irq_vector(cpt->pdev,129OTX_CPT_PF_INT_VEC_E_MBOXX(mbox_int_idx, 0)),130otx_cpt_mbx0_intr_handler, 0, "CPT Mbox0", cpt);131if (ret) {132dev_err(dev, "Request irq failed\n");133pci_free_irq_vectors(cpt->pdev);134return ret;135}136/* Enable mailbox interrupt */137otx_cpt_enable_mbox_interrupts(cpt);138return 0;139}140141static void otx_cpt_unregister_interrupts(struct otx_cpt_device *cpt)142{143u32 mbox_int_idx = OTX_CPT_PF_MBOX_INT;144145otx_cpt_disable_mbox_interrupts(cpt);146free_irq(pci_irq_vector(cpt->pdev,147OTX_CPT_PF_INT_VEC_E_MBOXX(mbox_int_idx, 0)),148cpt);149pci_free_irq_vectors(cpt->pdev);150}151152153static int otx_cpt_sriov_configure(struct pci_dev *pdev, int numvfs)154{155struct otx_cpt_device *cpt = pci_get_drvdata(pdev);156int ret = 0;157158if (numvfs > cpt->max_vfs)159numvfs = cpt->max_vfs;160161if (numvfs > 0) {162ret = otx_cpt_try_create_default_eng_grps(cpt->pdev,163&cpt->eng_grps,164cpt->pf_type);165if (ret)166return ret;167168cpt->vfs_enabled = numvfs;169ret = pci_enable_sriov(pdev, numvfs);170if (ret) {171cpt->vfs_enabled = 0;172return ret;173}174otx_cpt_set_eng_grps_is_rdonly(&cpt->eng_grps, true);175try_module_get(THIS_MODULE);176ret = numvfs;177} else {178pci_disable_sriov(pdev);179otx_cpt_set_eng_grps_is_rdonly(&cpt->eng_grps, false);180module_put(THIS_MODULE);181cpt->vfs_enabled = 0;182}183dev_notice(&cpt->pdev->dev, "VFs enabled: %d\n", ret);184185return ret;186}187188static int otx_cpt_probe(struct pci_dev *pdev,189const struct pci_device_id __always_unused *ent)190{191struct device *dev = &pdev->dev;192struct otx_cpt_device *cpt;193int err;194195cpt = devm_kzalloc(dev, sizeof(*cpt), GFP_KERNEL);196if (!cpt)197return -ENOMEM;198199pci_set_drvdata(pdev, cpt);200cpt->pdev = pdev;201202err = pci_enable_device(pdev);203if (err) {204dev_err(dev, "Failed to enable PCI device\n");205goto err_clear_drvdata;206}207208err = pci_request_regions(pdev, DRV_NAME);209if (err) {210dev_err(dev, "PCI request regions failed 0x%x\n", err);211goto err_disable_device;212}213214err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(48));215if (err) {216dev_err(dev, "Unable to get usable 48-bit DMA configuration\n");217goto err_release_regions;218}219220/* MAP PF's configuration registers */221cpt->reg_base = pci_iomap(pdev, OTX_CPT_PF_PCI_CFG_BAR, 0);222if (!cpt->reg_base) {223dev_err(dev, "Cannot map config register space, aborting\n");224err = -ENOMEM;225goto err_release_regions;226}227228/* CPT device HW initialization */229err = otx_cpt_device_init(cpt);230if (err)231goto err_unmap_region;232233/* Register interrupts */234err = otx_cpt_register_interrupts(cpt);235if (err)236goto err_unmap_region;237238/* Initialize engine groups */239err = otx_cpt_init_eng_grps(pdev, &cpt->eng_grps, cpt->pf_type);240if (err)241goto err_unregister_interrupts;242243return 0;244245err_unregister_interrupts:246otx_cpt_unregister_interrupts(cpt);247err_unmap_region:248pci_iounmap(pdev, cpt->reg_base);249err_release_regions:250pci_release_regions(pdev);251err_disable_device:252pci_disable_device(pdev);253err_clear_drvdata:254pci_set_drvdata(pdev, NULL);255256return err;257}258259static void otx_cpt_remove(struct pci_dev *pdev)260{261struct otx_cpt_device *cpt = pci_get_drvdata(pdev);262263if (!cpt)264return;265266/* Disable VFs */267pci_disable_sriov(pdev);268/* Cleanup engine groups */269otx_cpt_cleanup_eng_grps(pdev, &cpt->eng_grps);270/* Disable CPT PF interrupts */271otx_cpt_unregister_interrupts(cpt);272/* Disengage SE and AE cores from all groups */273otx_cpt_disable_all_cores(cpt);274pci_iounmap(pdev, cpt->reg_base);275pci_release_regions(pdev);276pci_disable_device(pdev);277pci_set_drvdata(pdev, NULL);278}279280/* Supported devices */281static const struct pci_device_id otx_cpt_id_table[] = {282{ PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, OTX_CPT_PCI_PF_DEVICE_ID) },283{ 0, } /* end of table */284};285286static struct pci_driver otx_cpt_pci_driver = {287.name = DRV_NAME,288.id_table = otx_cpt_id_table,289.probe = otx_cpt_probe,290.remove = otx_cpt_remove,291.sriov_configure = otx_cpt_sriov_configure292};293294module_pci_driver(otx_cpt_pci_driver);295296MODULE_AUTHOR("Marvell International Ltd.");297MODULE_DESCRIPTION("Marvell OcteonTX CPT Physical Function Driver");298MODULE_LICENSE("GPL v2");299MODULE_VERSION(DRV_VERSION);300MODULE_DEVICE_TABLE(pci, otx_cpt_id_table);301302303