1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /* Marvell OcteonTX CPT driver
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2019 Marvell International Ltd.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify
7*4882a593Smuzhiyun * it under the terms of the GNU General Public License version 2 as
8*4882a593Smuzhiyun * published by the Free Software Foundation.
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/delay.h>
12*4882a593Smuzhiyun #include "otx_cptvf.h"
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #define CPT_MBOX_MSG_TIMEOUT 2000
15*4882a593Smuzhiyun
get_mbox_opcode_str(int msg_opcode)16*4882a593Smuzhiyun static char *get_mbox_opcode_str(int msg_opcode)
17*4882a593Smuzhiyun {
18*4882a593Smuzhiyun char *str = "Unknown";
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun switch (msg_opcode) {
21*4882a593Smuzhiyun case OTX_CPT_MSG_VF_UP:
22*4882a593Smuzhiyun str = "UP";
23*4882a593Smuzhiyun break;
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun case OTX_CPT_MSG_VF_DOWN:
26*4882a593Smuzhiyun str = "DOWN";
27*4882a593Smuzhiyun break;
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun case OTX_CPT_MSG_READY:
30*4882a593Smuzhiyun str = "READY";
31*4882a593Smuzhiyun break;
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun case OTX_CPT_MSG_QLEN:
34*4882a593Smuzhiyun str = "QLEN";
35*4882a593Smuzhiyun break;
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun case OTX_CPT_MSG_QBIND_GRP:
38*4882a593Smuzhiyun str = "QBIND_GRP";
39*4882a593Smuzhiyun break;
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun case OTX_CPT_MSG_VQ_PRIORITY:
42*4882a593Smuzhiyun str = "VQ_PRIORITY";
43*4882a593Smuzhiyun break;
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun case OTX_CPT_MSG_PF_TYPE:
46*4882a593Smuzhiyun str = "PF_TYPE";
47*4882a593Smuzhiyun break;
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun case OTX_CPT_MSG_ACK:
50*4882a593Smuzhiyun str = "ACK";
51*4882a593Smuzhiyun break;
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun case OTX_CPT_MSG_NACK:
54*4882a593Smuzhiyun str = "NACK";
55*4882a593Smuzhiyun break;
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun return str;
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun
dump_mbox_msg(struct otx_cpt_mbox * mbox_msg,int vf_id)60*4882a593Smuzhiyun static void dump_mbox_msg(struct otx_cpt_mbox *mbox_msg, int vf_id)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun char raw_data_str[OTX_CPT_MAX_MBOX_DATA_STR_SIZE];
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun hex_dump_to_buffer(mbox_msg, sizeof(struct otx_cpt_mbox), 16, 8,
65*4882a593Smuzhiyun raw_data_str, OTX_CPT_MAX_MBOX_DATA_STR_SIZE, false);
66*4882a593Smuzhiyun if (vf_id >= 0)
67*4882a593Smuzhiyun pr_debug("MBOX msg %s received from VF%d raw_data %s",
68*4882a593Smuzhiyun get_mbox_opcode_str(mbox_msg->msg), vf_id,
69*4882a593Smuzhiyun raw_data_str);
70*4882a593Smuzhiyun else
71*4882a593Smuzhiyun pr_debug("MBOX msg %s received from PF raw_data %s",
72*4882a593Smuzhiyun get_mbox_opcode_str(mbox_msg->msg), raw_data_str);
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun
cptvf_send_msg_to_pf(struct otx_cptvf * cptvf,struct otx_cpt_mbox * mbx)75*4882a593Smuzhiyun static void cptvf_send_msg_to_pf(struct otx_cptvf *cptvf,
76*4882a593Smuzhiyun struct otx_cpt_mbox *mbx)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun /* Writing mbox(1) causes interrupt */
79*4882a593Smuzhiyun writeq(mbx->msg, cptvf->reg_base + OTX_CPT_VFX_PF_MBOXX(0, 0));
80*4882a593Smuzhiyun writeq(mbx->data, cptvf->reg_base + OTX_CPT_VFX_PF_MBOXX(0, 1));
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /* Interrupt handler to handle mailbox messages from VFs */
otx_cptvf_handle_mbox_intr(struct otx_cptvf * cptvf)84*4882a593Smuzhiyun void otx_cptvf_handle_mbox_intr(struct otx_cptvf *cptvf)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun struct otx_cpt_mbox mbx = {};
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun /*
89*4882a593Smuzhiyun * MBOX[0] contains msg
90*4882a593Smuzhiyun * MBOX[1] contains data
91*4882a593Smuzhiyun */
92*4882a593Smuzhiyun mbx.msg = readq(cptvf->reg_base + OTX_CPT_VFX_PF_MBOXX(0, 0));
93*4882a593Smuzhiyun mbx.data = readq(cptvf->reg_base + OTX_CPT_VFX_PF_MBOXX(0, 1));
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun dump_mbox_msg(&mbx, -1);
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun switch (mbx.msg) {
98*4882a593Smuzhiyun case OTX_CPT_MSG_VF_UP:
99*4882a593Smuzhiyun cptvf->pf_acked = true;
100*4882a593Smuzhiyun cptvf->num_vfs = mbx.data;
101*4882a593Smuzhiyun break;
102*4882a593Smuzhiyun case OTX_CPT_MSG_READY:
103*4882a593Smuzhiyun cptvf->pf_acked = true;
104*4882a593Smuzhiyun cptvf->vfid = mbx.data;
105*4882a593Smuzhiyun dev_dbg(&cptvf->pdev->dev, "Received VFID %d\n", cptvf->vfid);
106*4882a593Smuzhiyun break;
107*4882a593Smuzhiyun case OTX_CPT_MSG_QBIND_GRP:
108*4882a593Smuzhiyun cptvf->pf_acked = true;
109*4882a593Smuzhiyun cptvf->vftype = mbx.data;
110*4882a593Smuzhiyun dev_dbg(&cptvf->pdev->dev, "VF %d type %s group %d\n",
111*4882a593Smuzhiyun cptvf->vfid,
112*4882a593Smuzhiyun ((mbx.data == OTX_CPT_SE_TYPES) ? "SE" : "AE"),
113*4882a593Smuzhiyun cptvf->vfgrp);
114*4882a593Smuzhiyun break;
115*4882a593Smuzhiyun case OTX_CPT_MSG_ACK:
116*4882a593Smuzhiyun cptvf->pf_acked = true;
117*4882a593Smuzhiyun break;
118*4882a593Smuzhiyun case OTX_CPT_MSG_NACK:
119*4882a593Smuzhiyun cptvf->pf_nacked = true;
120*4882a593Smuzhiyun break;
121*4882a593Smuzhiyun default:
122*4882a593Smuzhiyun dev_err(&cptvf->pdev->dev, "Invalid msg from PF, msg 0x%llx\n",
123*4882a593Smuzhiyun mbx.msg);
124*4882a593Smuzhiyun break;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
cptvf_send_msg_to_pf_timeout(struct otx_cptvf * cptvf,struct otx_cpt_mbox * mbx)128*4882a593Smuzhiyun static int cptvf_send_msg_to_pf_timeout(struct otx_cptvf *cptvf,
129*4882a593Smuzhiyun struct otx_cpt_mbox *mbx)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun int timeout = CPT_MBOX_MSG_TIMEOUT;
132*4882a593Smuzhiyun int sleep = 10;
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun cptvf->pf_acked = false;
135*4882a593Smuzhiyun cptvf->pf_nacked = false;
136*4882a593Smuzhiyun cptvf_send_msg_to_pf(cptvf, mbx);
137*4882a593Smuzhiyun /* Wait for previous message to be acked, timeout 2sec */
138*4882a593Smuzhiyun while (!cptvf->pf_acked) {
139*4882a593Smuzhiyun if (cptvf->pf_nacked)
140*4882a593Smuzhiyun return -EINVAL;
141*4882a593Smuzhiyun msleep(sleep);
142*4882a593Smuzhiyun if (cptvf->pf_acked)
143*4882a593Smuzhiyun break;
144*4882a593Smuzhiyun timeout -= sleep;
145*4882a593Smuzhiyun if (!timeout) {
146*4882a593Smuzhiyun dev_err(&cptvf->pdev->dev,
147*4882a593Smuzhiyun "PF didn't ack to mbox msg %llx from VF%u\n",
148*4882a593Smuzhiyun mbx->msg, cptvf->vfid);
149*4882a593Smuzhiyun return -EBUSY;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun return 0;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /*
156*4882a593Smuzhiyun * Checks if VF is able to comminicate with PF
157*4882a593Smuzhiyun * and also gets the CPT number this VF is associated to.
158*4882a593Smuzhiyun */
otx_cptvf_check_pf_ready(struct otx_cptvf * cptvf)159*4882a593Smuzhiyun int otx_cptvf_check_pf_ready(struct otx_cptvf *cptvf)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun struct otx_cpt_mbox mbx = {};
162*4882a593Smuzhiyun int ret;
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun mbx.msg = OTX_CPT_MSG_READY;
165*4882a593Smuzhiyun ret = cptvf_send_msg_to_pf_timeout(cptvf, &mbx);
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun return ret;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun /*
171*4882a593Smuzhiyun * Communicate VQs size to PF to program CPT(0)_PF_Q(0-15)_CTL of the VF.
172*4882a593Smuzhiyun * Must be ACKed.
173*4882a593Smuzhiyun */
otx_cptvf_send_vq_size_msg(struct otx_cptvf * cptvf)174*4882a593Smuzhiyun int otx_cptvf_send_vq_size_msg(struct otx_cptvf *cptvf)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun struct otx_cpt_mbox mbx = {};
177*4882a593Smuzhiyun int ret;
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun mbx.msg = OTX_CPT_MSG_QLEN;
180*4882a593Smuzhiyun mbx.data = cptvf->qsize;
181*4882a593Smuzhiyun ret = cptvf_send_msg_to_pf_timeout(cptvf, &mbx);
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun return ret;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun /*
187*4882a593Smuzhiyun * Communicate VF group required to PF and get the VQ binded to that group
188*4882a593Smuzhiyun */
otx_cptvf_send_vf_to_grp_msg(struct otx_cptvf * cptvf,int group)189*4882a593Smuzhiyun int otx_cptvf_send_vf_to_grp_msg(struct otx_cptvf *cptvf, int group)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun struct otx_cpt_mbox mbx = {};
192*4882a593Smuzhiyun int ret;
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun mbx.msg = OTX_CPT_MSG_QBIND_GRP;
195*4882a593Smuzhiyun /* Convey group of the VF */
196*4882a593Smuzhiyun mbx.data = group;
197*4882a593Smuzhiyun ret = cptvf_send_msg_to_pf_timeout(cptvf, &mbx);
198*4882a593Smuzhiyun if (ret)
199*4882a593Smuzhiyun return ret;
200*4882a593Smuzhiyun cptvf->vfgrp = group;
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun return 0;
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun /*
206*4882a593Smuzhiyun * Communicate VF group required to PF and get the VQ binded to that group
207*4882a593Smuzhiyun */
otx_cptvf_send_vf_priority_msg(struct otx_cptvf * cptvf)208*4882a593Smuzhiyun int otx_cptvf_send_vf_priority_msg(struct otx_cptvf *cptvf)
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun struct otx_cpt_mbox mbx = {};
211*4882a593Smuzhiyun int ret;
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun mbx.msg = OTX_CPT_MSG_VQ_PRIORITY;
214*4882a593Smuzhiyun /* Convey group of the VF */
215*4882a593Smuzhiyun mbx.data = cptvf->priority;
216*4882a593Smuzhiyun ret = cptvf_send_msg_to_pf_timeout(cptvf, &mbx);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun return ret;
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun /*
222*4882a593Smuzhiyun * Communicate to PF that VF is UP and running
223*4882a593Smuzhiyun */
otx_cptvf_send_vf_up(struct otx_cptvf * cptvf)224*4882a593Smuzhiyun int otx_cptvf_send_vf_up(struct otx_cptvf *cptvf)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun struct otx_cpt_mbox mbx = {};
227*4882a593Smuzhiyun int ret;
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun mbx.msg = OTX_CPT_MSG_VF_UP;
230*4882a593Smuzhiyun ret = cptvf_send_msg_to_pf_timeout(cptvf, &mbx);
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun return ret;
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun /*
236*4882a593Smuzhiyun * Communicate to PF that VF is DOWN and running
237*4882a593Smuzhiyun */
otx_cptvf_send_vf_down(struct otx_cptvf * cptvf)238*4882a593Smuzhiyun int otx_cptvf_send_vf_down(struct otx_cptvf *cptvf)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun struct otx_cpt_mbox mbx = {};
241*4882a593Smuzhiyun int ret;
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun mbx.msg = OTX_CPT_MSG_VF_DOWN;
244*4882a593Smuzhiyun ret = cptvf_send_msg_to_pf_timeout(cptvf, &mbx);
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun return ret;
247*4882a593Smuzhiyun }
248