110b4f094SSrujanaChalla // SPDX-License-Identifier: GPL-2.0
210b4f094SSrujanaChalla /* Marvell OcteonTX CPT driver
310b4f094SSrujanaChalla *
410b4f094SSrujanaChalla * Copyright (C) 2019 Marvell International Ltd.
510b4f094SSrujanaChalla *
610b4f094SSrujanaChalla * This program is free software; you can redistribute it and/or modify
710b4f094SSrujanaChalla * it under the terms of the GNU General Public License version 2 as
810b4f094SSrujanaChalla * published by the Free Software Foundation.
910b4f094SSrujanaChalla */
1010b4f094SSrujanaChalla
1110b4f094SSrujanaChalla #include <linux/delay.h>
1210b4f094SSrujanaChalla #include "otx_cptvf.h"
1310b4f094SSrujanaChalla
1410b4f094SSrujanaChalla #define CPT_MBOX_MSG_TIMEOUT 2000
1510b4f094SSrujanaChalla
get_mbox_opcode_str(int msg_opcode)1610b4f094SSrujanaChalla static char *get_mbox_opcode_str(int msg_opcode)
1710b4f094SSrujanaChalla {
1810b4f094SSrujanaChalla char *str = "Unknown";
1910b4f094SSrujanaChalla
2010b4f094SSrujanaChalla switch (msg_opcode) {
2110b4f094SSrujanaChalla case OTX_CPT_MSG_VF_UP:
2210b4f094SSrujanaChalla str = "UP";
2310b4f094SSrujanaChalla break;
2410b4f094SSrujanaChalla
2510b4f094SSrujanaChalla case OTX_CPT_MSG_VF_DOWN:
2610b4f094SSrujanaChalla str = "DOWN";
2710b4f094SSrujanaChalla break;
2810b4f094SSrujanaChalla
2910b4f094SSrujanaChalla case OTX_CPT_MSG_READY:
3010b4f094SSrujanaChalla str = "READY";
3110b4f094SSrujanaChalla break;
3210b4f094SSrujanaChalla
3310b4f094SSrujanaChalla case OTX_CPT_MSG_QLEN:
3410b4f094SSrujanaChalla str = "QLEN";
3510b4f094SSrujanaChalla break;
3610b4f094SSrujanaChalla
3710b4f094SSrujanaChalla case OTX_CPT_MSG_QBIND_GRP:
3810b4f094SSrujanaChalla str = "QBIND_GRP";
3910b4f094SSrujanaChalla break;
4010b4f094SSrujanaChalla
4110b4f094SSrujanaChalla case OTX_CPT_MSG_VQ_PRIORITY:
4210b4f094SSrujanaChalla str = "VQ_PRIORITY";
4310b4f094SSrujanaChalla break;
4410b4f094SSrujanaChalla
4510b4f094SSrujanaChalla case OTX_CPT_MSG_PF_TYPE:
4610b4f094SSrujanaChalla str = "PF_TYPE";
4710b4f094SSrujanaChalla break;
4810b4f094SSrujanaChalla
4910b4f094SSrujanaChalla case OTX_CPT_MSG_ACK:
5010b4f094SSrujanaChalla str = "ACK";
5110b4f094SSrujanaChalla break;
5210b4f094SSrujanaChalla
5310b4f094SSrujanaChalla case OTX_CPT_MSG_NACK:
5410b4f094SSrujanaChalla str = "NACK";
5510b4f094SSrujanaChalla break;
5610b4f094SSrujanaChalla }
5710b4f094SSrujanaChalla return str;
5810b4f094SSrujanaChalla }
5910b4f094SSrujanaChalla
dump_mbox_msg(struct otx_cpt_mbox * mbox_msg,int vf_id)6010b4f094SSrujanaChalla static void dump_mbox_msg(struct otx_cpt_mbox *mbox_msg, int vf_id)
6110b4f094SSrujanaChalla {
6210b4f094SSrujanaChalla char raw_data_str[OTX_CPT_MAX_MBOX_DATA_STR_SIZE];
6310b4f094SSrujanaChalla
6410b4f094SSrujanaChalla hex_dump_to_buffer(mbox_msg, sizeof(struct otx_cpt_mbox), 16, 8,
6510b4f094SSrujanaChalla raw_data_str, OTX_CPT_MAX_MBOX_DATA_STR_SIZE, false);
6610b4f094SSrujanaChalla if (vf_id >= 0)
6710b4f094SSrujanaChalla pr_debug("MBOX msg %s received from VF%d raw_data %s",
6810b4f094SSrujanaChalla get_mbox_opcode_str(mbox_msg->msg), vf_id,
6910b4f094SSrujanaChalla raw_data_str);
7010b4f094SSrujanaChalla else
7110b4f094SSrujanaChalla pr_debug("MBOX msg %s received from PF raw_data %s",
7210b4f094SSrujanaChalla get_mbox_opcode_str(mbox_msg->msg), raw_data_str);
7310b4f094SSrujanaChalla }
7410b4f094SSrujanaChalla
cptvf_send_msg_to_pf(struct otx_cptvf * cptvf,struct otx_cpt_mbox * mbx)7510b4f094SSrujanaChalla static void cptvf_send_msg_to_pf(struct otx_cptvf *cptvf,
7610b4f094SSrujanaChalla struct otx_cpt_mbox *mbx)
7710b4f094SSrujanaChalla {
7810b4f094SSrujanaChalla /* Writing mbox(1) causes interrupt */
7910b4f094SSrujanaChalla writeq(mbx->msg, cptvf->reg_base + OTX_CPT_VFX_PF_MBOXX(0, 0));
8010b4f094SSrujanaChalla writeq(mbx->data, cptvf->reg_base + OTX_CPT_VFX_PF_MBOXX(0, 1));
8110b4f094SSrujanaChalla }
8210b4f094SSrujanaChalla
8310b4f094SSrujanaChalla /* Interrupt handler to handle mailbox messages from VFs */
otx_cptvf_handle_mbox_intr(struct otx_cptvf * cptvf)8410b4f094SSrujanaChalla void otx_cptvf_handle_mbox_intr(struct otx_cptvf *cptvf)
8510b4f094SSrujanaChalla {
8610b4f094SSrujanaChalla struct otx_cpt_mbox mbx = {};
8710b4f094SSrujanaChalla
8810b4f094SSrujanaChalla /*
8910b4f094SSrujanaChalla * MBOX[0] contains msg
9010b4f094SSrujanaChalla * MBOX[1] contains data
9110b4f094SSrujanaChalla */
9210b4f094SSrujanaChalla mbx.msg = readq(cptvf->reg_base + OTX_CPT_VFX_PF_MBOXX(0, 0));
9310b4f094SSrujanaChalla mbx.data = readq(cptvf->reg_base + OTX_CPT_VFX_PF_MBOXX(0, 1));
9410b4f094SSrujanaChalla
9510b4f094SSrujanaChalla dump_mbox_msg(&mbx, -1);
9610b4f094SSrujanaChalla
9710b4f094SSrujanaChalla switch (mbx.msg) {
9810b4f094SSrujanaChalla case OTX_CPT_MSG_VF_UP:
9910b4f094SSrujanaChalla cptvf->pf_acked = true;
10010b4f094SSrujanaChalla cptvf->num_vfs = mbx.data;
10110b4f094SSrujanaChalla break;
10210b4f094SSrujanaChalla case OTX_CPT_MSG_READY:
10310b4f094SSrujanaChalla cptvf->pf_acked = true;
10410b4f094SSrujanaChalla cptvf->vfid = mbx.data;
10510b4f094SSrujanaChalla dev_dbg(&cptvf->pdev->dev, "Received VFID %d\n", cptvf->vfid);
10610b4f094SSrujanaChalla break;
10710b4f094SSrujanaChalla case OTX_CPT_MSG_QBIND_GRP:
10810b4f094SSrujanaChalla cptvf->pf_acked = true;
10910b4f094SSrujanaChalla cptvf->vftype = mbx.data;
11010b4f094SSrujanaChalla dev_dbg(&cptvf->pdev->dev, "VF %d type %s group %d\n",
11110b4f094SSrujanaChalla cptvf->vfid,
11210b4f094SSrujanaChalla ((mbx.data == OTX_CPT_SE_TYPES) ? "SE" : "AE"),
11310b4f094SSrujanaChalla cptvf->vfgrp);
11410b4f094SSrujanaChalla break;
11510b4f094SSrujanaChalla case OTX_CPT_MSG_ACK:
11610b4f094SSrujanaChalla cptvf->pf_acked = true;
11710b4f094SSrujanaChalla break;
11810b4f094SSrujanaChalla case OTX_CPT_MSG_NACK:
11910b4f094SSrujanaChalla cptvf->pf_nacked = true;
12010b4f094SSrujanaChalla break;
12110b4f094SSrujanaChalla default:
12210b4f094SSrujanaChalla dev_err(&cptvf->pdev->dev, "Invalid msg from PF, msg 0x%llx\n",
12310b4f094SSrujanaChalla mbx.msg);
12410b4f094SSrujanaChalla break;
12510b4f094SSrujanaChalla }
12610b4f094SSrujanaChalla }
12710b4f094SSrujanaChalla
cptvf_send_msg_to_pf_timeout(struct otx_cptvf * cptvf,struct otx_cpt_mbox * mbx)12810b4f094SSrujanaChalla static int cptvf_send_msg_to_pf_timeout(struct otx_cptvf *cptvf,
12910b4f094SSrujanaChalla struct otx_cpt_mbox *mbx)
13010b4f094SSrujanaChalla {
13110b4f094SSrujanaChalla int timeout = CPT_MBOX_MSG_TIMEOUT;
13210b4f094SSrujanaChalla int sleep = 10;
13310b4f094SSrujanaChalla
13410b4f094SSrujanaChalla cptvf->pf_acked = false;
13510b4f094SSrujanaChalla cptvf->pf_nacked = false;
13610b4f094SSrujanaChalla cptvf_send_msg_to_pf(cptvf, mbx);
13710b4f094SSrujanaChalla /* Wait for previous message to be acked, timeout 2sec */
13810b4f094SSrujanaChalla while (!cptvf->pf_acked) {
13910b4f094SSrujanaChalla if (cptvf->pf_nacked)
14010b4f094SSrujanaChalla return -EINVAL;
14110b4f094SSrujanaChalla msleep(sleep);
14210b4f094SSrujanaChalla if (cptvf->pf_acked)
14310b4f094SSrujanaChalla break;
14410b4f094SSrujanaChalla timeout -= sleep;
14510b4f094SSrujanaChalla if (!timeout) {
14610b4f094SSrujanaChalla dev_err(&cptvf->pdev->dev,
14710b4f094SSrujanaChalla "PF didn't ack to mbox msg %llx from VF%u\n",
14810b4f094SSrujanaChalla mbx->msg, cptvf->vfid);
14910b4f094SSrujanaChalla return -EBUSY;
15010b4f094SSrujanaChalla }
15110b4f094SSrujanaChalla }
15210b4f094SSrujanaChalla return 0;
15310b4f094SSrujanaChalla }
15410b4f094SSrujanaChalla
15510b4f094SSrujanaChalla /*
15610b4f094SSrujanaChalla * Checks if VF is able to comminicate with PF
15710b4f094SSrujanaChalla * and also gets the CPT number this VF is associated to.
15810b4f094SSrujanaChalla */
otx_cptvf_check_pf_ready(struct otx_cptvf * cptvf)15910b4f094SSrujanaChalla int otx_cptvf_check_pf_ready(struct otx_cptvf *cptvf)
16010b4f094SSrujanaChalla {
16110b4f094SSrujanaChalla struct otx_cpt_mbox mbx = {};
16210b4f094SSrujanaChalla
16310b4f094SSrujanaChalla mbx.msg = OTX_CPT_MSG_READY;
16410b4f094SSrujanaChalla
165*24ddd4e1Sye xingchen return cptvf_send_msg_to_pf_timeout(cptvf, &mbx);
16610b4f094SSrujanaChalla }
16710b4f094SSrujanaChalla
16810b4f094SSrujanaChalla /*
16910b4f094SSrujanaChalla * Communicate VQs size to PF to program CPT(0)_PF_Q(0-15)_CTL of the VF.
17010b4f094SSrujanaChalla * Must be ACKed.
17110b4f094SSrujanaChalla */
otx_cptvf_send_vq_size_msg(struct otx_cptvf * cptvf)17210b4f094SSrujanaChalla int otx_cptvf_send_vq_size_msg(struct otx_cptvf *cptvf)
17310b4f094SSrujanaChalla {
17410b4f094SSrujanaChalla struct otx_cpt_mbox mbx = {};
17510b4f094SSrujanaChalla
17610b4f094SSrujanaChalla mbx.msg = OTX_CPT_MSG_QLEN;
17710b4f094SSrujanaChalla mbx.data = cptvf->qsize;
17810b4f094SSrujanaChalla
179*24ddd4e1Sye xingchen return cptvf_send_msg_to_pf_timeout(cptvf, &mbx);
18010b4f094SSrujanaChalla }
18110b4f094SSrujanaChalla
18210b4f094SSrujanaChalla /*
18310b4f094SSrujanaChalla * Communicate VF group required to PF and get the VQ binded to that group
18410b4f094SSrujanaChalla */
otx_cptvf_send_vf_to_grp_msg(struct otx_cptvf * cptvf,int group)18510b4f094SSrujanaChalla int otx_cptvf_send_vf_to_grp_msg(struct otx_cptvf *cptvf, int group)
18610b4f094SSrujanaChalla {
18710b4f094SSrujanaChalla struct otx_cpt_mbox mbx = {};
18810b4f094SSrujanaChalla int ret;
18910b4f094SSrujanaChalla
19010b4f094SSrujanaChalla mbx.msg = OTX_CPT_MSG_QBIND_GRP;
19110b4f094SSrujanaChalla /* Convey group of the VF */
19210b4f094SSrujanaChalla mbx.data = group;
19310b4f094SSrujanaChalla ret = cptvf_send_msg_to_pf_timeout(cptvf, &mbx);
19410b4f094SSrujanaChalla if (ret)
19510b4f094SSrujanaChalla return ret;
19610b4f094SSrujanaChalla cptvf->vfgrp = group;
19710b4f094SSrujanaChalla
19810b4f094SSrujanaChalla return 0;
19910b4f094SSrujanaChalla }
20010b4f094SSrujanaChalla
20110b4f094SSrujanaChalla /*
20210b4f094SSrujanaChalla * Communicate VF group required to PF and get the VQ binded to that group
20310b4f094SSrujanaChalla */
otx_cptvf_send_vf_priority_msg(struct otx_cptvf * cptvf)20410b4f094SSrujanaChalla int otx_cptvf_send_vf_priority_msg(struct otx_cptvf *cptvf)
20510b4f094SSrujanaChalla {
20610b4f094SSrujanaChalla struct otx_cpt_mbox mbx = {};
20710b4f094SSrujanaChalla
20810b4f094SSrujanaChalla mbx.msg = OTX_CPT_MSG_VQ_PRIORITY;
20910b4f094SSrujanaChalla /* Convey group of the VF */
21010b4f094SSrujanaChalla mbx.data = cptvf->priority;
21110b4f094SSrujanaChalla
212*24ddd4e1Sye xingchen return cptvf_send_msg_to_pf_timeout(cptvf, &mbx);
21310b4f094SSrujanaChalla }
21410b4f094SSrujanaChalla
21510b4f094SSrujanaChalla /*
21610b4f094SSrujanaChalla * Communicate to PF that VF is UP and running
21710b4f094SSrujanaChalla */
otx_cptvf_send_vf_up(struct otx_cptvf * cptvf)21810b4f094SSrujanaChalla int otx_cptvf_send_vf_up(struct otx_cptvf *cptvf)
21910b4f094SSrujanaChalla {
22010b4f094SSrujanaChalla struct otx_cpt_mbox mbx = {};
22110b4f094SSrujanaChalla
22210b4f094SSrujanaChalla mbx.msg = OTX_CPT_MSG_VF_UP;
22310b4f094SSrujanaChalla
224*24ddd4e1Sye xingchen return cptvf_send_msg_to_pf_timeout(cptvf, &mbx);
22510b4f094SSrujanaChalla }
22610b4f094SSrujanaChalla
22710b4f094SSrujanaChalla /*
22810b4f094SSrujanaChalla * Communicate to PF that VF is DOWN and running
22910b4f094SSrujanaChalla */
otx_cptvf_send_vf_down(struct otx_cptvf * cptvf)23010b4f094SSrujanaChalla int otx_cptvf_send_vf_down(struct otx_cptvf *cptvf)
23110b4f094SSrujanaChalla {
23210b4f094SSrujanaChalla struct otx_cpt_mbox mbx = {};
23310b4f094SSrujanaChalla
23410b4f094SSrujanaChalla mbx.msg = OTX_CPT_MSG_VF_DOWN;
23510b4f094SSrujanaChalla
236*24ddd4e1Sye xingchen return cptvf_send_msg_to_pf_timeout(cptvf, &mbx);
23710b4f094SSrujanaChalla }
238