Path: blob/master/drivers/crypto/marvell/octeontx/otx_cptvf_mbox.c
26288 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 <linux/delay.h>11#include "otx_cptvf.h"1213#define CPT_MBOX_MSG_TIMEOUT 20001415static char *get_mbox_opcode_str(int msg_opcode)16{17char *str = "Unknown";1819switch (msg_opcode) {20case OTX_CPT_MSG_VF_UP:21str = "UP";22break;2324case OTX_CPT_MSG_VF_DOWN:25str = "DOWN";26break;2728case OTX_CPT_MSG_READY:29str = "READY";30break;3132case OTX_CPT_MSG_QLEN:33str = "QLEN";34break;3536case OTX_CPT_MSG_QBIND_GRP:37str = "QBIND_GRP";38break;3940case OTX_CPT_MSG_VQ_PRIORITY:41str = "VQ_PRIORITY";42break;4344case OTX_CPT_MSG_PF_TYPE:45str = "PF_TYPE";46break;4748case OTX_CPT_MSG_ACK:49str = "ACK";50break;5152case OTX_CPT_MSG_NACK:53str = "NACK";54break;55}56return str;57}5859static void dump_mbox_msg(struct otx_cpt_mbox *mbox_msg, int vf_id)60{61char raw_data_str[OTX_CPT_MAX_MBOX_DATA_STR_SIZE];6263hex_dump_to_buffer(mbox_msg, sizeof(struct otx_cpt_mbox), 16, 8,64raw_data_str, OTX_CPT_MAX_MBOX_DATA_STR_SIZE, false);65if (vf_id >= 0)66pr_debug("MBOX msg %s received from VF%d raw_data %s",67get_mbox_opcode_str(mbox_msg->msg), vf_id,68raw_data_str);69else70pr_debug("MBOX msg %s received from PF raw_data %s",71get_mbox_opcode_str(mbox_msg->msg), raw_data_str);72}7374static void cptvf_send_msg_to_pf(struct otx_cptvf *cptvf,75struct otx_cpt_mbox *mbx)76{77/* Writing mbox(1) causes interrupt */78writeq(mbx->msg, cptvf->reg_base + OTX_CPT_VFX_PF_MBOXX(0, 0));79writeq(mbx->data, cptvf->reg_base + OTX_CPT_VFX_PF_MBOXX(0, 1));80}8182/* Interrupt handler to handle mailbox messages from VFs */83void otx_cptvf_handle_mbox_intr(struct otx_cptvf *cptvf)84{85struct otx_cpt_mbox mbx = {};8687/*88* MBOX[0] contains msg89* MBOX[1] contains data90*/91mbx.msg = readq(cptvf->reg_base + OTX_CPT_VFX_PF_MBOXX(0, 0));92mbx.data = readq(cptvf->reg_base + OTX_CPT_VFX_PF_MBOXX(0, 1));9394dump_mbox_msg(&mbx, -1);9596switch (mbx.msg) {97case OTX_CPT_MSG_VF_UP:98cptvf->pf_acked = true;99cptvf->num_vfs = mbx.data;100break;101case OTX_CPT_MSG_READY:102cptvf->pf_acked = true;103cptvf->vfid = mbx.data;104dev_dbg(&cptvf->pdev->dev, "Received VFID %d\n", cptvf->vfid);105break;106case OTX_CPT_MSG_QBIND_GRP:107cptvf->pf_acked = true;108cptvf->vftype = mbx.data;109dev_dbg(&cptvf->pdev->dev, "VF %d type %s group %d\n",110cptvf->vfid,111((mbx.data == OTX_CPT_SE_TYPES) ? "SE" : "AE"),112cptvf->vfgrp);113break;114case OTX_CPT_MSG_ACK:115cptvf->pf_acked = true;116break;117case OTX_CPT_MSG_NACK:118cptvf->pf_nacked = true;119break;120default:121dev_err(&cptvf->pdev->dev, "Invalid msg from PF, msg 0x%llx\n",122mbx.msg);123break;124}125}126127static int cptvf_send_msg_to_pf_timeout(struct otx_cptvf *cptvf,128struct otx_cpt_mbox *mbx)129{130int timeout = CPT_MBOX_MSG_TIMEOUT;131int sleep = 10;132133cptvf->pf_acked = false;134cptvf->pf_nacked = false;135cptvf_send_msg_to_pf(cptvf, mbx);136/* Wait for previous message to be acked, timeout 2sec */137while (!cptvf->pf_acked) {138if (cptvf->pf_nacked)139return -EINVAL;140msleep(sleep);141if (cptvf->pf_acked)142break;143timeout -= sleep;144if (!timeout) {145dev_err(&cptvf->pdev->dev,146"PF didn't ack to mbox msg %llx from VF%u\n",147mbx->msg, cptvf->vfid);148return -EBUSY;149}150}151return 0;152}153154/*155* Checks if VF is able to comminicate with PF156* and also gets the CPT number this VF is associated to.157*/158int otx_cptvf_check_pf_ready(struct otx_cptvf *cptvf)159{160struct otx_cpt_mbox mbx = {};161162mbx.msg = OTX_CPT_MSG_READY;163164return cptvf_send_msg_to_pf_timeout(cptvf, &mbx);165}166167/*168* Communicate VQs size to PF to program CPT(0)_PF_Q(0-15)_CTL of the VF.169* Must be ACKed.170*/171int otx_cptvf_send_vq_size_msg(struct otx_cptvf *cptvf)172{173struct otx_cpt_mbox mbx = {};174175mbx.msg = OTX_CPT_MSG_QLEN;176mbx.data = cptvf->qsize;177178return cptvf_send_msg_to_pf_timeout(cptvf, &mbx);179}180181/*182* Communicate VF group required to PF and get the VQ binded to that group183*/184int otx_cptvf_send_vf_to_grp_msg(struct otx_cptvf *cptvf, int group)185{186struct otx_cpt_mbox mbx = {};187int ret;188189mbx.msg = OTX_CPT_MSG_QBIND_GRP;190/* Convey group of the VF */191mbx.data = group;192ret = cptvf_send_msg_to_pf_timeout(cptvf, &mbx);193if (ret)194return ret;195cptvf->vfgrp = group;196197return 0;198}199200/*201* Communicate VF group required to PF and get the VQ binded to that group202*/203int otx_cptvf_send_vf_priority_msg(struct otx_cptvf *cptvf)204{205struct otx_cpt_mbox mbx = {};206207mbx.msg = OTX_CPT_MSG_VQ_PRIORITY;208/* Convey group of the VF */209mbx.data = cptvf->priority;210211return cptvf_send_msg_to_pf_timeout(cptvf, &mbx);212}213214/*215* Communicate to PF that VF is UP and running216*/217int otx_cptvf_send_vf_up(struct otx_cptvf *cptvf)218{219struct otx_cpt_mbox mbx = {};220221mbx.msg = OTX_CPT_MSG_VF_UP;222223return cptvf_send_msg_to_pf_timeout(cptvf, &mbx);224}225226/*227* Communicate to PF that VF is DOWN and running228*/229int otx_cptvf_send_vf_down(struct otx_cptvf *cptvf)230{231struct otx_cpt_mbox mbx = {};232233mbx.msg = OTX_CPT_MSG_VF_DOWN;234235return cptvf_send_msg_to_pf_timeout(cptvf, &mbx);236}237238239