xref: /dpdk/drivers/crypto/octeontx/otx_cryptodev_mbox.c (revision 13d711f353c109ca93f2649d0935660824f25176)
1ae500541SMurthy NSSR /* SPDX-License-Identifier: BSD-3-Clause
2ae500541SMurthy NSSR  * Copyright(c) 2018 Cavium, Inc
3ae500541SMurthy NSSR  */
4ae500541SMurthy NSSR 
5ae500541SMurthy NSSR #include <unistd.h>
6ae500541SMurthy NSSR 
7ae500541SMurthy NSSR #include "otx_cryptodev_hw_access.h"
8ae500541SMurthy NSSR #include "otx_cryptodev_mbox.h"
9ae500541SMurthy NSSR 
10ae500541SMurthy NSSR void
otx_cpt_handle_mbox_intr(struct cpt_vf * cptvf)11ae500541SMurthy NSSR otx_cpt_handle_mbox_intr(struct cpt_vf *cptvf)
12ae500541SMurthy NSSR {
13ae500541SMurthy NSSR 	struct cpt_mbox mbx = {0, 0};
14ae500541SMurthy NSSR 
15ae500541SMurthy NSSR 	/*
16ae500541SMurthy NSSR 	 * MBOX[0] contains msg
17ae500541SMurthy NSSR 	 * MBOX[1] contains data
18ae500541SMurthy NSSR 	 */
19ae500541SMurthy NSSR 	mbx.msg  = CPT_READ_CSR(CPT_CSR_REG_BASE(cptvf),
20ae500541SMurthy NSSR 				CPTX_VFX_PF_MBOXX(0, 0, 0));
21ae500541SMurthy NSSR 	mbx.data = CPT_READ_CSR(CPT_CSR_REG_BASE(cptvf),
22ae500541SMurthy NSSR 				CPTX_VFX_PF_MBOXX(0, 0, 1));
23ae500541SMurthy NSSR 
24ae500541SMurthy NSSR 	CPT_LOG_DP_DEBUG("%s: Mailbox msg 0x%lx from PF",
25ae500541SMurthy NSSR 		    cptvf->dev_name, (unsigned int long)mbx.msg);
26ae500541SMurthy NSSR 	switch (mbx.msg) {
27c6fa0d2fSLukasz Bartosik 	case OTX_CPT_MSG_VF_UP:
28c6fa0d2fSLukasz Bartosik 		cptvf->pf_acked = true;
29c6fa0d2fSLukasz Bartosik 		break;
30ae500541SMurthy NSSR 	case OTX_CPT_MSG_READY:
31ae500541SMurthy NSSR 		{
32ae500541SMurthy NSSR 			otx_cpt_chipid_vfid_t cid;
33ae500541SMurthy NSSR 
34ae500541SMurthy NSSR 			cid.u64 = mbx.data;
35ae500541SMurthy NSSR 			cptvf->pf_acked = true;
36ae500541SMurthy NSSR 			cptvf->vfid = cid.s.vfid;
37ae500541SMurthy NSSR 			CPT_LOG_DP_DEBUG("%s: Received VFID %d chip_id %d",
38ae500541SMurthy NSSR 					 cptvf->dev_name,
39ae500541SMurthy NSSR 					 cptvf->vfid, cid.s.chip_id);
40ae500541SMurthy NSSR 		}
41ae500541SMurthy NSSR 		break;
42ae500541SMurthy NSSR 	case OTX_CPT_MSG_QBIND_GRP:
43ae500541SMurthy NSSR 		cptvf->pf_acked = true;
44ae500541SMurthy NSSR 		cptvf->vftype = mbx.data;
45*13d711f3SKanaka Durga Kotamarthy 		CPT_LOG_DP_DEBUG("%s: VF %d group %d",
46ae500541SMurthy NSSR 				 cptvf->dev_name, cptvf->vfid,
47ae500541SMurthy NSSR 				 cptvf->vfgrp);
48ae500541SMurthy NSSR 		break;
49*13d711f3SKanaka Durga Kotamarthy 	case OTX_CPT_MSG_PF_TYPE:
50*13d711f3SKanaka Durga Kotamarthy 		cptvf->pf_acked = true;
51*13d711f3SKanaka Durga Kotamarthy 		if (mbx.data == OTX_CPT_PF_TYPE_AE)
52*13d711f3SKanaka Durga Kotamarthy 			cptvf->vftype = OTX_CPT_VF_TYPE_AE;
53*13d711f3SKanaka Durga Kotamarthy 		else if (mbx.data == OTX_CPT_PF_TYPE_SE)
54*13d711f3SKanaka Durga Kotamarthy 			cptvf->vftype = OTX_CPT_VF_TYPE_SE;
55*13d711f3SKanaka Durga Kotamarthy 		else
56*13d711f3SKanaka Durga Kotamarthy 			cptvf->vftype = OTX_CPT_VF_TYPE_INVALID;
57*13d711f3SKanaka Durga Kotamarthy 		break;
58ae500541SMurthy NSSR 	case OTX_CPT_MBOX_MSG_TYPE_ACK:
59ae500541SMurthy NSSR 		cptvf->pf_acked = true;
60ae500541SMurthy NSSR 		break;
61ae500541SMurthy NSSR 	case OTX_CPT_MBOX_MSG_TYPE_NACK:
62ae500541SMurthy NSSR 		cptvf->pf_nacked = true;
63ae500541SMurthy NSSR 		break;
64ae500541SMurthy NSSR 	default:
65ae500541SMurthy NSSR 		CPT_LOG_DP_DEBUG("%s: Invalid msg from PF, msg 0x%lx",
66ae500541SMurthy NSSR 				 cptvf->dev_name, (unsigned int long)mbx.msg);
67ae500541SMurthy NSSR 		break;
68ae500541SMurthy NSSR 	}
69ae500541SMurthy NSSR }
70ae500541SMurthy NSSR 
71ae500541SMurthy NSSR /* Send a mailbox message to PF
72ae500541SMurthy NSSR  * @vf: vf from which this message to be sent
73ae500541SMurthy NSSR  * @mbx: Message to be sent
74ae500541SMurthy NSSR  */
75ae500541SMurthy NSSR static void
otx_cpt_send_msg_to_pf(struct cpt_vf * cptvf,struct cpt_mbox * mbx)76ae500541SMurthy NSSR otx_cpt_send_msg_to_pf(struct cpt_vf *cptvf, struct cpt_mbox *mbx)
77ae500541SMurthy NSSR {
78ae500541SMurthy NSSR 	/* Writing mbox(1) causes interrupt */
79ae500541SMurthy NSSR 	CPT_WRITE_CSR(CPT_CSR_REG_BASE(cptvf),
80ae500541SMurthy NSSR 		      CPTX_VFX_PF_MBOXX(0, 0, 0), mbx->msg);
81ae500541SMurthy NSSR 	CPT_WRITE_CSR(CPT_CSR_REG_BASE(cptvf),
82ae500541SMurthy NSSR 		      CPTX_VFX_PF_MBOXX(0, 0, 1), mbx->data);
83ae500541SMurthy NSSR }
84ae500541SMurthy NSSR 
85ae500541SMurthy NSSR static int32_t
otx_cpt_send_msg_to_pf_timeout(struct cpt_vf * cptvf,struct cpt_mbox * mbx)86ae500541SMurthy NSSR otx_cpt_send_msg_to_pf_timeout(struct cpt_vf *cptvf, struct cpt_mbox *mbx)
87ae500541SMurthy NSSR {
88ae500541SMurthy NSSR 	int timeout = OTX_CPT_MBOX_MSG_TIMEOUT;
89ae500541SMurthy NSSR 	int sleep_ms = 10;
90ae500541SMurthy NSSR 
91ae500541SMurthy NSSR 	cptvf->pf_acked = false;
92ae500541SMurthy NSSR 	cptvf->pf_nacked = false;
93ae500541SMurthy NSSR 
94ae500541SMurthy NSSR 	otx_cpt_send_msg_to_pf(cptvf, mbx);
95ae500541SMurthy NSSR 
96ae500541SMurthy NSSR 	/* Wait for previous message to be acked, timeout 2sec */
97ae500541SMurthy NSSR 	while (!cptvf->pf_acked) {
98ae500541SMurthy NSSR 		if (cptvf->pf_nacked)
99ae500541SMurthy NSSR 			return -EINVAL;
100ae500541SMurthy NSSR 		usleep(sleep_ms * 1000);
101ae500541SMurthy NSSR 		otx_cpt_poll_misc(cptvf);
102ae500541SMurthy NSSR 		if (cptvf->pf_acked)
103ae500541SMurthy NSSR 			break;
104ae500541SMurthy NSSR 		timeout -= sleep_ms;
105ae500541SMurthy NSSR 		if (!timeout) {
106ae500541SMurthy NSSR 			CPT_LOG_ERR("%s: PF didn't ack mbox msg %lx(vfid %u)",
107ae500541SMurthy NSSR 				    cptvf->dev_name,
108ae500541SMurthy NSSR 				    (unsigned int long)(mbx->msg & 0xFF),
109ae500541SMurthy NSSR 				    cptvf->vfid);
110ae500541SMurthy NSSR 			return -EBUSY;
111ae500541SMurthy NSSR 		}
112ae500541SMurthy NSSR 	}
113ae500541SMurthy NSSR 	return 0;
114ae500541SMurthy NSSR }
115ae500541SMurthy NSSR 
116ae500541SMurthy NSSR int
otx_cpt_check_pf_ready(struct cpt_vf * cptvf)117ae500541SMurthy NSSR otx_cpt_check_pf_ready(struct cpt_vf *cptvf)
118ae500541SMurthy NSSR {
119ae500541SMurthy NSSR 	struct cpt_mbox mbx = {0, 0};
120ae500541SMurthy NSSR 
121ae500541SMurthy NSSR 	mbx.msg = OTX_CPT_MSG_READY;
122ae500541SMurthy NSSR 	if (otx_cpt_send_msg_to_pf_timeout(cptvf, &mbx)) {
123ae500541SMurthy NSSR 		CPT_LOG_ERR("%s: PF didn't respond to READY msg",
124ae500541SMurthy NSSR 			    cptvf->dev_name);
125ae500541SMurthy NSSR 		return 1;
126ae500541SMurthy NSSR 	}
127ae500541SMurthy NSSR 	return 0;
128ae500541SMurthy NSSR }
129ae500541SMurthy NSSR 
130ae500541SMurthy NSSR int
otx_cpt_get_dev_type(struct cpt_vf * cptvf)131*13d711f3SKanaka Durga Kotamarthy otx_cpt_get_dev_type(struct cpt_vf *cptvf)
132*13d711f3SKanaka Durga Kotamarthy {
133*13d711f3SKanaka Durga Kotamarthy 	struct cpt_mbox mbx = {0, 0};
134*13d711f3SKanaka Durga Kotamarthy 
135*13d711f3SKanaka Durga Kotamarthy 	mbx.msg = OTX_CPT_MSG_PF_TYPE;
136*13d711f3SKanaka Durga Kotamarthy 	if (otx_cpt_send_msg_to_pf_timeout(cptvf, &mbx)) {
137*13d711f3SKanaka Durga Kotamarthy 		CPT_LOG_ERR("%s: PF didn't respond to query msg",
138*13d711f3SKanaka Durga Kotamarthy 			    cptvf->dev_name);
139*13d711f3SKanaka Durga Kotamarthy 		return 1;
140*13d711f3SKanaka Durga Kotamarthy 	}
141*13d711f3SKanaka Durga Kotamarthy 	return 0;
142*13d711f3SKanaka Durga Kotamarthy }
143*13d711f3SKanaka Durga Kotamarthy 
144*13d711f3SKanaka Durga Kotamarthy int
otx_cpt_send_vq_size_msg(struct cpt_vf * cptvf)145ae500541SMurthy NSSR otx_cpt_send_vq_size_msg(struct cpt_vf *cptvf)
146ae500541SMurthy NSSR {
147ae500541SMurthy NSSR 	struct cpt_mbox mbx = {0, 0};
148ae500541SMurthy NSSR 
149ae500541SMurthy NSSR 	mbx.msg = OTX_CPT_MSG_QLEN;
150ae500541SMurthy NSSR 
151ae500541SMurthy NSSR 	mbx.data = cptvf->qsize;
152ae500541SMurthy NSSR 	if (otx_cpt_send_msg_to_pf_timeout(cptvf, &mbx)) {
153ae500541SMurthy NSSR 		CPT_LOG_ERR("%s: PF didn't respond to vq_size msg",
154ae500541SMurthy NSSR 			    cptvf->dev_name);
155ae500541SMurthy NSSR 		return 1;
156ae500541SMurthy NSSR 	}
157ae500541SMurthy NSSR 	return 0;
158ae500541SMurthy NSSR }
159ae500541SMurthy NSSR 
160ae500541SMurthy NSSR int
otx_cpt_send_vf_grp_msg(struct cpt_vf * cptvf,uint32_t group)161ae500541SMurthy NSSR otx_cpt_send_vf_grp_msg(struct cpt_vf *cptvf, uint32_t group)
162ae500541SMurthy NSSR {
163ae500541SMurthy NSSR 	struct cpt_mbox mbx = {0, 0};
164ae500541SMurthy NSSR 
165ae500541SMurthy NSSR 	mbx.msg = OTX_CPT_MSG_QBIND_GRP;
166ae500541SMurthy NSSR 
167ae500541SMurthy NSSR 	/* Convey group of the VF */
168ae500541SMurthy NSSR 	mbx.data = group;
169ae500541SMurthy NSSR 	if (otx_cpt_send_msg_to_pf_timeout(cptvf, &mbx)) {
170ae500541SMurthy NSSR 		CPT_LOG_ERR("%s: PF didn't respond to vf_type msg",
171ae500541SMurthy NSSR 			    cptvf->dev_name);
172ae500541SMurthy NSSR 		return 1;
173ae500541SMurthy NSSR 	}
174ae500541SMurthy NSSR 	return 0;
175ae500541SMurthy NSSR }
176ae500541SMurthy NSSR 
177ae500541SMurthy NSSR int
otx_cpt_send_vf_up(struct cpt_vf * cptvf)178ae500541SMurthy NSSR otx_cpt_send_vf_up(struct cpt_vf *cptvf)
179ae500541SMurthy NSSR {
180ae500541SMurthy NSSR 	struct cpt_mbox mbx = {0, 0};
181ae500541SMurthy NSSR 
182ae500541SMurthy NSSR 	mbx.msg = OTX_CPT_MSG_VF_UP;
183ae500541SMurthy NSSR 	if (otx_cpt_send_msg_to_pf_timeout(cptvf, &mbx)) {
184ae500541SMurthy NSSR 		CPT_LOG_ERR("%s: PF didn't respond to UP msg",
185ae500541SMurthy NSSR 			    cptvf->dev_name);
186ae500541SMurthy NSSR 		return 1;
187ae500541SMurthy NSSR 	}
188ae500541SMurthy NSSR 	return 0;
189ae500541SMurthy NSSR }
190ae500541SMurthy NSSR 
191ae500541SMurthy NSSR int
otx_cpt_send_vf_down(struct cpt_vf * cptvf)192ae500541SMurthy NSSR otx_cpt_send_vf_down(struct cpt_vf *cptvf)
193ae500541SMurthy NSSR {
194ae500541SMurthy NSSR 	struct cpt_mbox mbx = {0, 0};
195ae500541SMurthy NSSR 
196ae500541SMurthy NSSR 	mbx.msg = OTX_CPT_MSG_VF_DOWN;
197ae500541SMurthy NSSR 	if (otx_cpt_send_msg_to_pf_timeout(cptvf, &mbx)) {
198ae500541SMurthy NSSR 		CPT_LOG_ERR("%s: PF didn't respond to DOWN msg",
199ae500541SMurthy NSSR 			    cptvf->dev_name);
200ae500541SMurthy NSSR 		return 1;
201ae500541SMurthy NSSR 	}
202ae500541SMurthy NSSR 	return 0;
203ae500541SMurthy NSSR }
204