Path: blob/master/drivers/crypto/cavium/nitrox/nitrox_main.c
29281 views
// SPDX-License-Identifier: GPL-2.0-only1#include <linux/delay.h>2#include <linux/firmware.h>3#include <linux/list.h>4#include <linux/module.h>5#include <linux/mutex.h>6#include <linux/pci.h>7#include <linux/pci_ids.h>89#include "nitrox_dev.h"10#include "nitrox_common.h"11#include "nitrox_csr.h"12#include "nitrox_hal.h"13#include "nitrox_isr.h"14#include "nitrox_debugfs.h"1516#define CNN55XX_DEV_ID 0x1217#define UCODE_HLEN 4818#define DEFAULT_SE_GROUP 019#define DEFAULT_AE_GROUP 02021#define DRIVER_VERSION "1.2"22#define CNN55XX_UCD_BLOCK_SIZE 3276823#define CNN55XX_MAX_UCODE_SIZE (CNN55XX_UCD_BLOCK_SIZE * 2)24#define FW_DIR "cavium/"25/* SE microcode */26#define SE_FW FW_DIR "cnn55xx_se.fw"27/* AE microcode */28#define AE_FW FW_DIR "cnn55xx_ae.fw"2930static const char nitrox_driver_name[] = "CNN55XX";3132static LIST_HEAD(ndevlist);33static DEFINE_MUTEX(devlist_lock);34static unsigned int num_devices;3536/*37* nitrox_pci_tbl - PCI Device ID Table38*/39static const struct pci_device_id nitrox_pci_tbl[] = {40{PCI_VDEVICE(CAVIUM, CNN55XX_DEV_ID), 0},41/* required last entry */42{0, }43};44MODULE_DEVICE_TABLE(pci, nitrox_pci_tbl);4546static unsigned int qlen = DEFAULT_CMD_QLEN;47module_param(qlen, uint, 0644);48MODULE_PARM_DESC(qlen, "Command queue length - default 2048");4950/**51* struct ucode - Firmware Header52* @id: microcode ID53* @version: firmware version54* @code_size: code section size55* @raz: alignment56* @code: code section57*/58struct ucode {59u8 id;60char version[VERSION_LEN - 1];61__be32 code_size;62u8 raz[12];63u64 code[];64};6566/*67* write_to_ucd_unit - Write Firmware to NITROX UCD unit68*/69static void write_to_ucd_unit(struct nitrox_device *ndev, u32 ucode_size,70u64 *ucode_data, int block_num)71{72u32 code_size;73u64 offset, data;74int i = 0;7576/*77* UCD structure78*79* -------------80* | BLK 7 |81* -------------82* | BLK 6 |83* -------------84* | ... |85* -------------86* | BLK 0 |87* -------------88* Total of 8 blocks, each size 32KB89*/9091/* set the block number */92offset = UCD_UCODE_LOAD_BLOCK_NUM;93nitrox_write_csr(ndev, offset, block_num);9495code_size = roundup(ucode_size, 16);96while (code_size) {97data = ucode_data[i];98/* write 8 bytes at a time */99offset = UCD_UCODE_LOAD_IDX_DATAX(i);100nitrox_write_csr(ndev, offset, data);101code_size -= 8;102i++;103}104105usleep_range(300, 400);106}107108static int nitrox_load_fw(struct nitrox_device *ndev)109{110const struct firmware *fw;111const char *fw_name;112struct ucode *ucode;113u64 *ucode_data;114u64 offset;115union ucd_core_eid_ucode_block_num core_2_eid_val;116union aqm_grp_execmsk_lo aqm_grp_execmask_lo;117union aqm_grp_execmsk_hi aqm_grp_execmask_hi;118u32 ucode_size;119int ret, i = 0;120121fw_name = SE_FW;122dev_info(DEV(ndev), "Loading firmware \"%s\"\n", fw_name);123124ret = request_firmware(&fw, fw_name, DEV(ndev));125if (ret < 0) {126dev_err(DEV(ndev), "failed to get firmware %s\n", fw_name);127return ret;128}129130ucode = (struct ucode *)fw->data;131132ucode_size = be32_to_cpu(ucode->code_size) * 2;133if (!ucode_size || ucode_size > CNN55XX_MAX_UCODE_SIZE) {134dev_err(DEV(ndev), "Invalid ucode size: %u for firmware %s\n",135ucode_size, fw_name);136release_firmware(fw);137return -EINVAL;138}139ucode_data = ucode->code;140141/* copy the firmware version */142memcpy(&ndev->hw.fw_name[0][0], ucode->version, (VERSION_LEN - 2));143ndev->hw.fw_name[0][VERSION_LEN - 1] = '\0';144145/* Load SE Firmware on UCD Block 0 */146write_to_ucd_unit(ndev, ucode_size, ucode_data, 0);147148release_firmware(fw);149150/* put all SE cores in DEFAULT_SE_GROUP */151offset = POM_GRP_EXECMASKX(DEFAULT_SE_GROUP);152nitrox_write_csr(ndev, offset, (~0ULL));153154/* write block number and firmware length155* bit:<2:0> block number156* bit:3 is set SE uses 32KB microcode157* bit:3 is clear SE uses 64KB microcode158*/159core_2_eid_val.value = 0ULL;160core_2_eid_val.ucode_blk = 0;161if (ucode_size <= CNN55XX_UCD_BLOCK_SIZE)162core_2_eid_val.ucode_len = 1;163else164core_2_eid_val.ucode_len = 0;165166for (i = 0; i < ndev->hw.se_cores; i++) {167offset = UCD_SE_EID_UCODE_BLOCK_NUMX(i);168nitrox_write_csr(ndev, offset, core_2_eid_val.value);169}170171172fw_name = AE_FW;173dev_info(DEV(ndev), "Loading firmware \"%s\"\n", fw_name);174175ret = request_firmware(&fw, fw_name, DEV(ndev));176if (ret < 0) {177dev_err(DEV(ndev), "failed to get firmware %s\n", fw_name);178return ret;179}180181ucode = (struct ucode *)fw->data;182183ucode_size = be32_to_cpu(ucode->code_size) * 2;184if (!ucode_size || ucode_size > CNN55XX_MAX_UCODE_SIZE) {185dev_err(DEV(ndev), "Invalid ucode size: %u for firmware %s\n",186ucode_size, fw_name);187release_firmware(fw);188return -EINVAL;189}190ucode_data = ucode->code;191192/* copy the firmware version */193memcpy(&ndev->hw.fw_name[1][0], ucode->version, (VERSION_LEN - 2));194ndev->hw.fw_name[1][VERSION_LEN - 1] = '\0';195196/* Load AE Firmware on UCD Block 2 */197write_to_ucd_unit(ndev, ucode_size, ucode_data, 2);198199release_firmware(fw);200201/* put all AE cores in DEFAULT_AE_GROUP */202offset = AQM_GRP_EXECMSK_LOX(DEFAULT_AE_GROUP);203aqm_grp_execmask_lo.exec_0_to_39 = 0xFFFFFFFFFFULL;204nitrox_write_csr(ndev, offset, aqm_grp_execmask_lo.value);205offset = AQM_GRP_EXECMSK_HIX(DEFAULT_AE_GROUP);206aqm_grp_execmask_hi.exec_40_to_79 = 0xFFFFFFFFFFULL;207nitrox_write_csr(ndev, offset, aqm_grp_execmask_hi.value);208209/* write block number and firmware length210* bit:<2:0> block number211* bit:3 is set AE uses 32KB microcode212* bit:3 is clear AE uses 64KB microcode213*/214core_2_eid_val.value = 0ULL;215core_2_eid_val.ucode_blk = 2;216if (ucode_size <= CNN55XX_UCD_BLOCK_SIZE)217core_2_eid_val.ucode_len = 1;218else219core_2_eid_val.ucode_len = 0;220221for (i = 0; i < ndev->hw.ae_cores; i++) {222offset = UCD_AE_EID_UCODE_BLOCK_NUMX(i);223nitrox_write_csr(ndev, offset, core_2_eid_val.value);224}225226return 0;227}228229/**230* nitrox_add_to_devlist - add NITROX device to global device list231* @ndev: NITROX device232*/233static int nitrox_add_to_devlist(struct nitrox_device *ndev)234{235struct nitrox_device *dev;236int ret = 0;237238INIT_LIST_HEAD(&ndev->list);239refcount_set(&ndev->refcnt, 1);240241mutex_lock(&devlist_lock);242list_for_each_entry(dev, &ndevlist, list) {243if (dev == ndev) {244ret = -EEXIST;245goto unlock;246}247}248ndev->idx = num_devices++;249list_add_tail(&ndev->list, &ndevlist);250unlock:251mutex_unlock(&devlist_lock);252return ret;253}254255/**256* nitrox_remove_from_devlist - remove NITROX device from257* global device list258* @ndev: NITROX device259*/260static void nitrox_remove_from_devlist(struct nitrox_device *ndev)261{262mutex_lock(&devlist_lock);263list_del(&ndev->list);264num_devices--;265mutex_unlock(&devlist_lock);266}267268struct nitrox_device *nitrox_get_first_device(void)269{270struct nitrox_device *ndev = NULL, *iter;271272mutex_lock(&devlist_lock);273list_for_each_entry(iter, &ndevlist, list) {274if (nitrox_ready(iter)) {275ndev = iter;276break;277}278}279mutex_unlock(&devlist_lock);280if (!ndev)281return NULL;282283refcount_inc(&ndev->refcnt);284/* barrier to sync with other cpus */285smp_mb__after_atomic();286return ndev;287}288289void nitrox_put_device(struct nitrox_device *ndev)290{291if (!ndev)292return;293294refcount_dec(&ndev->refcnt);295/* barrier to sync with other cpus */296smp_mb__after_atomic();297}298299static int nitrox_device_flr(struct pci_dev *pdev)300{301int pos = 0;302303pos = pci_save_state(pdev);304if (pos) {305dev_err(&pdev->dev, "Failed to save pci state\n");306return -ENOMEM;307}308309pcie_reset_flr(pdev, PCI_RESET_DO_RESET);310311pci_restore_state(pdev);312313return 0;314}315316static int nitrox_pf_sw_init(struct nitrox_device *ndev)317{318int err;319320err = nitrox_common_sw_init(ndev);321if (err)322return err;323324err = nitrox_register_interrupts(ndev);325if (err)326nitrox_common_sw_cleanup(ndev);327328return err;329}330331static void nitrox_pf_sw_cleanup(struct nitrox_device *ndev)332{333nitrox_unregister_interrupts(ndev);334nitrox_common_sw_cleanup(ndev);335}336337/**338* nitrox_bist_check - Check NITROX BIST registers status339* @ndev: NITROX device340*/341static int nitrox_bist_check(struct nitrox_device *ndev)342{343u64 value = 0;344int i;345346for (i = 0; i < NR_CLUSTERS; i++) {347value += nitrox_read_csr(ndev, EMU_BIST_STATUSX(i));348value += nitrox_read_csr(ndev, EFL_CORE_BIST_REGX(i));349}350value += nitrox_read_csr(ndev, UCD_BIST_STATUS);351value += nitrox_read_csr(ndev, NPS_CORE_BIST_REG);352value += nitrox_read_csr(ndev, NPS_CORE_NPC_BIST_REG);353value += nitrox_read_csr(ndev, NPS_PKT_SLC_BIST_REG);354value += nitrox_read_csr(ndev, NPS_PKT_IN_BIST_REG);355value += nitrox_read_csr(ndev, POM_BIST_REG);356value += nitrox_read_csr(ndev, BMI_BIST_REG);357value += nitrox_read_csr(ndev, EFL_TOP_BIST_STAT);358value += nitrox_read_csr(ndev, BMO_BIST_REG);359value += nitrox_read_csr(ndev, LBC_BIST_STATUS);360value += nitrox_read_csr(ndev, PEM_BIST_STATUSX(0));361if (value)362return -EIO;363return 0;364}365366static int nitrox_pf_hw_init(struct nitrox_device *ndev)367{368int err;369370err = nitrox_bist_check(ndev);371if (err) {372dev_err(&ndev->pdev->dev, "BIST check failed\n");373return err;374}375/* get cores information */376nitrox_get_hwinfo(ndev);377378nitrox_config_nps_core_unit(ndev);379nitrox_config_aqm_unit(ndev);380nitrox_config_nps_pkt_unit(ndev);381nitrox_config_pom_unit(ndev);382nitrox_config_efl_unit(ndev);383/* configure IO units */384nitrox_config_bmi_unit(ndev);385nitrox_config_bmo_unit(ndev);386/* configure Local Buffer Cache */387nitrox_config_lbc_unit(ndev);388nitrox_config_rand_unit(ndev);389390/* load firmware on cores */391err = nitrox_load_fw(ndev);392if (err)393return err;394395nitrox_config_emu_unit(ndev);396397return 0;398}399400/**401* nitrox_probe - NITROX Initialization function.402* @pdev: PCI device information struct403* @id: entry in nitrox_pci_tbl404*405* Return: 0, if the driver is bound to the device, or406* a negative error if there is failure.407*/408static int nitrox_probe(struct pci_dev *pdev,409const struct pci_device_id *id)410{411struct nitrox_device *ndev;412int err;413414dev_info_once(&pdev->dev, "%s driver version %s\n",415nitrox_driver_name, DRIVER_VERSION);416417err = pci_enable_device_mem(pdev);418if (err)419return err;420421/* do FLR */422err = nitrox_device_flr(pdev);423if (err) {424dev_err(&pdev->dev, "FLR failed\n");425goto flr_fail;426}427428if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {429dev_dbg(&pdev->dev, "DMA to 64-BIT address\n");430} else {431err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));432if (err) {433dev_err(&pdev->dev, "DMA configuration failed\n");434goto flr_fail;435}436}437438err = pci_request_mem_regions(pdev, nitrox_driver_name);439if (err)440goto flr_fail;441pci_set_master(pdev);442443ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);444if (!ndev) {445err = -ENOMEM;446goto ndev_fail;447}448449pci_set_drvdata(pdev, ndev);450ndev->pdev = pdev;451452/* add to device list */453nitrox_add_to_devlist(ndev);454455ndev->hw.vendor_id = pdev->vendor;456ndev->hw.device_id = pdev->device;457ndev->hw.revision_id = pdev->revision;458/* command timeout in jiffies */459ndev->timeout = msecs_to_jiffies(CMD_TIMEOUT);460ndev->node = dev_to_node(&pdev->dev);461if (ndev->node == NUMA_NO_NODE)462ndev->node = 0;463464ndev->bar_addr = ioremap(pci_resource_start(pdev, 0),465pci_resource_len(pdev, 0));466if (!ndev->bar_addr) {467err = -EIO;468goto ioremap_err;469}470/* allocate command queus based on cpus, max queues are 64 */471ndev->nr_queues = min_t(u32, MAX_PF_QUEUES, num_online_cpus());472ndev->qlen = qlen;473474err = nitrox_pf_sw_init(ndev);475if (err)476goto pf_sw_fail;477478err = nitrox_pf_hw_init(ndev);479if (err)480goto pf_hw_fail;481482nitrox_debugfs_init(ndev);483484/* clear the statistics */485atomic64_set(&ndev->stats.posted, 0);486atomic64_set(&ndev->stats.completed, 0);487atomic64_set(&ndev->stats.dropped, 0);488489atomic_set(&ndev->state, __NDEV_READY);490/* barrier to sync with other cpus */491smp_mb__after_atomic();492493err = nitrox_crypto_register();494if (err)495goto crypto_fail;496497return 0;498499crypto_fail:500nitrox_debugfs_exit(ndev);501atomic_set(&ndev->state, __NDEV_NOT_READY);502/* barrier to sync with other cpus */503smp_mb__after_atomic();504pf_hw_fail:505nitrox_pf_sw_cleanup(ndev);506pf_sw_fail:507iounmap(ndev->bar_addr);508ioremap_err:509nitrox_remove_from_devlist(ndev);510kfree(ndev);511pci_set_drvdata(pdev, NULL);512ndev_fail:513pci_release_mem_regions(pdev);514flr_fail:515pci_disable_device(pdev);516return err;517}518519/**520* nitrox_remove - Unbind the driver from the device.521* @pdev: PCI device information struct522*/523static void nitrox_remove(struct pci_dev *pdev)524{525struct nitrox_device *ndev = pci_get_drvdata(pdev);526527if (!ndev)528return;529530if (!refcount_dec_and_test(&ndev->refcnt)) {531dev_err(DEV(ndev), "Device refcnt not zero (%d)\n",532refcount_read(&ndev->refcnt));533return;534}535536dev_info(DEV(ndev), "Removing Device %x:%x\n",537ndev->hw.vendor_id, ndev->hw.device_id);538539atomic_set(&ndev->state, __NDEV_NOT_READY);540/* barrier to sync with other cpus */541smp_mb__after_atomic();542543nitrox_remove_from_devlist(ndev);544545/* disable SR-IOV */546nitrox_sriov_configure(pdev, 0);547nitrox_crypto_unregister();548nitrox_debugfs_exit(ndev);549nitrox_pf_sw_cleanup(ndev);550551iounmap(ndev->bar_addr);552kfree(ndev);553554pci_set_drvdata(pdev, NULL);555pci_release_mem_regions(pdev);556pci_disable_device(pdev);557}558559static void nitrox_shutdown(struct pci_dev *pdev)560{561pci_set_drvdata(pdev, NULL);562pci_release_mem_regions(pdev);563pci_disable_device(pdev);564}565566static struct pci_driver nitrox_driver = {567.name = nitrox_driver_name,568.id_table = nitrox_pci_tbl,569.probe = nitrox_probe,570.remove = nitrox_remove,571.shutdown = nitrox_shutdown,572.sriov_configure = nitrox_sriov_configure,573};574575module_pci_driver(nitrox_driver);576577MODULE_AUTHOR("Srikanth Jampala <[email protected]>");578MODULE_DESCRIPTION("Cavium CNN55XX PF Driver" DRIVER_VERSION " ");579MODULE_LICENSE("GPL");580MODULE_VERSION(DRIVER_VERSION);581MODULE_FIRMWARE(SE_FW);582583584