xref: /dpdk/drivers/crypto/octeontx/otx_cryptodev_mbox.c (revision c6fa0d2f6c9aaba4b970d345c84af22f593a0f89)
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
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) {
27*c6fa0d2fSLukasz Bartosik 	case OTX_CPT_MSG_VF_UP:
28*c6fa0d2fSLukasz Bartosik 		cptvf->pf_acked = true;
29*c6fa0d2fSLukasz 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;
45ae500541SMurthy NSSR 		CPT_LOG_DP_DEBUG("%s: VF %d type %s group %d",
46ae500541SMurthy NSSR 				 cptvf->dev_name, cptvf->vfid,
47ae500541SMurthy NSSR 				 ((mbx.data == SE_TYPE) ? "SE" : "AE"),
48ae500541SMurthy NSSR 				 cptvf->vfgrp);
49ae500541SMurthy NSSR 		break;
50ae500541SMurthy NSSR 	case OTX_CPT_MBOX_MSG_TYPE_ACK:
51ae500541SMurthy NSSR 		cptvf->pf_acked = true;
52ae500541SMurthy NSSR 		break;
53ae500541SMurthy NSSR 	case OTX_CPT_MBOX_MSG_TYPE_NACK:
54ae500541SMurthy NSSR 		cptvf->pf_nacked = true;
55ae500541SMurthy NSSR 		break;
56ae500541SMurthy NSSR 	default:
57ae500541SMurthy NSSR 		CPT_LOG_DP_DEBUG("%s: Invalid msg from PF, msg 0x%lx",
58ae500541SMurthy NSSR 				 cptvf->dev_name, (unsigned int long)mbx.msg);
59ae500541SMurthy NSSR 		break;
60ae500541SMurthy NSSR 	}
61ae500541SMurthy NSSR }
62ae500541SMurthy NSSR 
63ae500541SMurthy NSSR /* Send a mailbox message to PF
64ae500541SMurthy NSSR  * @vf: vf from which this message to be sent
65ae500541SMurthy NSSR  * @mbx: Message to be sent
66ae500541SMurthy NSSR  */
67ae500541SMurthy NSSR static void
68ae500541SMurthy NSSR otx_cpt_send_msg_to_pf(struct cpt_vf *cptvf, struct cpt_mbox *mbx)
69ae500541SMurthy NSSR {
70ae500541SMurthy NSSR 	/* Writing mbox(1) causes interrupt */
71ae500541SMurthy NSSR 	CPT_WRITE_CSR(CPT_CSR_REG_BASE(cptvf),
72ae500541SMurthy NSSR 		      CPTX_VFX_PF_MBOXX(0, 0, 0), mbx->msg);
73ae500541SMurthy NSSR 	CPT_WRITE_CSR(CPT_CSR_REG_BASE(cptvf),
74ae500541SMurthy NSSR 		      CPTX_VFX_PF_MBOXX(0, 0, 1), mbx->data);
75ae500541SMurthy NSSR }
76ae500541SMurthy NSSR 
77ae500541SMurthy NSSR static int32_t
78ae500541SMurthy NSSR otx_cpt_send_msg_to_pf_timeout(struct cpt_vf *cptvf, struct cpt_mbox *mbx)
79ae500541SMurthy NSSR {
80ae500541SMurthy NSSR 	int timeout = OTX_CPT_MBOX_MSG_TIMEOUT;
81ae500541SMurthy NSSR 	int sleep_ms = 10;
82ae500541SMurthy NSSR 
83ae500541SMurthy NSSR 	cptvf->pf_acked = false;
84ae500541SMurthy NSSR 	cptvf->pf_nacked = false;
85ae500541SMurthy NSSR 
86ae500541SMurthy NSSR 	otx_cpt_send_msg_to_pf(cptvf, mbx);
87ae500541SMurthy NSSR 
88ae500541SMurthy NSSR 	/* Wait for previous message to be acked, timeout 2sec */
89ae500541SMurthy NSSR 	while (!cptvf->pf_acked) {
90ae500541SMurthy NSSR 		if (cptvf->pf_nacked)
91ae500541SMurthy NSSR 			return -EINVAL;
92ae500541SMurthy NSSR 		usleep(sleep_ms * 1000);
93ae500541SMurthy NSSR 		otx_cpt_poll_misc(cptvf);
94ae500541SMurthy NSSR 		if (cptvf->pf_acked)
95ae500541SMurthy NSSR 			break;
96ae500541SMurthy NSSR 		timeout -= sleep_ms;
97ae500541SMurthy NSSR 		if (!timeout) {
98ae500541SMurthy NSSR 			CPT_LOG_ERR("%s: PF didn't ack mbox msg %lx(vfid %u)",
99ae500541SMurthy NSSR 				    cptvf->dev_name,
100ae500541SMurthy NSSR 				    (unsigned int long)(mbx->msg & 0xFF),
101ae500541SMurthy NSSR 				    cptvf->vfid);
102ae500541SMurthy NSSR 			return -EBUSY;
103ae500541SMurthy NSSR 		}
104ae500541SMurthy NSSR 	}
105ae500541SMurthy NSSR 	return 0;
106ae500541SMurthy NSSR }
107ae500541SMurthy NSSR 
108ae500541SMurthy NSSR int
109ae500541SMurthy NSSR otx_cpt_check_pf_ready(struct cpt_vf *cptvf)
110ae500541SMurthy NSSR {
111ae500541SMurthy NSSR 	struct cpt_mbox mbx = {0, 0};
112ae500541SMurthy NSSR 
113ae500541SMurthy NSSR 	mbx.msg = OTX_CPT_MSG_READY;
114ae500541SMurthy NSSR 	if (otx_cpt_send_msg_to_pf_timeout(cptvf, &mbx)) {
115ae500541SMurthy NSSR 		CPT_LOG_ERR("%s: PF didn't respond to READY msg",
116ae500541SMurthy NSSR 			    cptvf->dev_name);
117ae500541SMurthy NSSR 		return 1;
118ae500541SMurthy NSSR 	}
119ae500541SMurthy NSSR 	return 0;
120ae500541SMurthy NSSR }
121ae500541SMurthy NSSR 
122ae500541SMurthy NSSR int
123ae500541SMurthy NSSR otx_cpt_send_vq_size_msg(struct cpt_vf *cptvf)
124ae500541SMurthy NSSR {
125ae500541SMurthy NSSR 	struct cpt_mbox mbx = {0, 0};
126ae500541SMurthy NSSR 
127ae500541SMurthy NSSR 	mbx.msg = OTX_CPT_MSG_QLEN;
128ae500541SMurthy NSSR 
129ae500541SMurthy NSSR 	mbx.data = cptvf->qsize;
130ae500541SMurthy NSSR 	if (otx_cpt_send_msg_to_pf_timeout(cptvf, &mbx)) {
131ae500541SMurthy NSSR 		CPT_LOG_ERR("%s: PF didn't respond to vq_size msg",
132ae500541SMurthy NSSR 			    cptvf->dev_name);
133ae500541SMurthy NSSR 		return 1;
134ae500541SMurthy NSSR 	}
135ae500541SMurthy NSSR 	return 0;
136ae500541SMurthy NSSR }
137ae500541SMurthy NSSR 
138ae500541SMurthy NSSR int
139ae500541SMurthy NSSR otx_cpt_send_vf_grp_msg(struct cpt_vf *cptvf, uint32_t group)
140ae500541SMurthy NSSR {
141ae500541SMurthy NSSR 	struct cpt_mbox mbx = {0, 0};
142ae500541SMurthy NSSR 
143ae500541SMurthy NSSR 	mbx.msg = OTX_CPT_MSG_QBIND_GRP;
144ae500541SMurthy NSSR 
145ae500541SMurthy NSSR 	/* Convey group of the VF */
146ae500541SMurthy NSSR 	mbx.data = group;
147ae500541SMurthy NSSR 	if (otx_cpt_send_msg_to_pf_timeout(cptvf, &mbx)) {
148ae500541SMurthy NSSR 		CPT_LOG_ERR("%s: PF didn't respond to vf_type msg",
149ae500541SMurthy NSSR 			    cptvf->dev_name);
150ae500541SMurthy NSSR 		return 1;
151ae500541SMurthy NSSR 	}
152ae500541SMurthy NSSR 	return 0;
153ae500541SMurthy NSSR }
154ae500541SMurthy NSSR 
155ae500541SMurthy NSSR int
156ae500541SMurthy NSSR otx_cpt_send_vf_up(struct cpt_vf *cptvf)
157ae500541SMurthy NSSR {
158ae500541SMurthy NSSR 	struct cpt_mbox mbx = {0, 0};
159ae500541SMurthy NSSR 
160ae500541SMurthy NSSR 	mbx.msg = OTX_CPT_MSG_VF_UP;
161ae500541SMurthy NSSR 	if (otx_cpt_send_msg_to_pf_timeout(cptvf, &mbx)) {
162ae500541SMurthy NSSR 		CPT_LOG_ERR("%s: PF didn't respond to UP msg",
163ae500541SMurthy NSSR 			    cptvf->dev_name);
164ae500541SMurthy NSSR 		return 1;
165ae500541SMurthy NSSR 	}
166ae500541SMurthy NSSR 	return 0;
167ae500541SMurthy NSSR }
168ae500541SMurthy NSSR 
169ae500541SMurthy NSSR int
170ae500541SMurthy NSSR otx_cpt_send_vf_down(struct cpt_vf *cptvf)
171ae500541SMurthy NSSR {
172ae500541SMurthy NSSR 	struct cpt_mbox mbx = {0, 0};
173ae500541SMurthy NSSR 
174ae500541SMurthy NSSR 	mbx.msg = OTX_CPT_MSG_VF_DOWN;
175ae500541SMurthy NSSR 	if (otx_cpt_send_msg_to_pf_timeout(cptvf, &mbx)) {
176ae500541SMurthy NSSR 		CPT_LOG_ERR("%s: PF didn't respond to DOWN msg",
177ae500541SMurthy NSSR 			    cptvf->dev_name);
178ae500541SMurthy NSSR 		return 1;
179ae500541SMurthy NSSR 	}
180ae500541SMurthy NSSR 	return 0;
181ae500541SMurthy NSSR }
182