xref: /OK3568_Linux_fs/kernel/drivers/crypto/cavium/nitrox/nitrox_mbx.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include <linux/workqueue.h>
3*4882a593Smuzhiyun 
4*4882a593Smuzhiyun #include "nitrox_csr.h"
5*4882a593Smuzhiyun #include "nitrox_hal.h"
6*4882a593Smuzhiyun #include "nitrox_dev.h"
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #define RING_TO_VFNO(_x, _y)	((_x) / (_y))
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun /**
11*4882a593Smuzhiyun  * mbx_msg_type - Mailbox message types
12*4882a593Smuzhiyun  */
13*4882a593Smuzhiyun enum mbx_msg_type {
14*4882a593Smuzhiyun 	MBX_MSG_TYPE_NOP,
15*4882a593Smuzhiyun 	MBX_MSG_TYPE_REQ,
16*4882a593Smuzhiyun 	MBX_MSG_TYPE_ACK,
17*4882a593Smuzhiyun 	MBX_MSG_TYPE_NACK,
18*4882a593Smuzhiyun };
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun /**
21*4882a593Smuzhiyun  * mbx_msg_opcode - Mailbox message opcodes
22*4882a593Smuzhiyun  */
23*4882a593Smuzhiyun enum mbx_msg_opcode {
24*4882a593Smuzhiyun 	MSG_OP_VF_MODE = 1,
25*4882a593Smuzhiyun 	MSG_OP_VF_UP,
26*4882a593Smuzhiyun 	MSG_OP_VF_DOWN,
27*4882a593Smuzhiyun 	MSG_OP_CHIPID_VFID,
28*4882a593Smuzhiyun 	MSG_OP_MCODE_INFO = 11,
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun struct pf2vf_work {
32*4882a593Smuzhiyun 	struct nitrox_vfdev *vfdev;
33*4882a593Smuzhiyun 	struct nitrox_device *ndev;
34*4882a593Smuzhiyun 	struct work_struct pf2vf_resp;
35*4882a593Smuzhiyun };
36*4882a593Smuzhiyun 
pf2vf_read_mbox(struct nitrox_device * ndev,int ring)37*4882a593Smuzhiyun static inline u64 pf2vf_read_mbox(struct nitrox_device *ndev, int ring)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun 	u64 reg_addr;
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	reg_addr = NPS_PKT_MBOX_VF_PF_PFDATAX(ring);
42*4882a593Smuzhiyun 	return nitrox_read_csr(ndev, reg_addr);
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun 
pf2vf_write_mbox(struct nitrox_device * ndev,u64 value,int ring)45*4882a593Smuzhiyun static inline void pf2vf_write_mbox(struct nitrox_device *ndev, u64 value,
46*4882a593Smuzhiyun 				    int ring)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun 	u64 reg_addr;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	reg_addr = NPS_PKT_MBOX_PF_VF_PFDATAX(ring);
51*4882a593Smuzhiyun 	nitrox_write_csr(ndev, reg_addr, value);
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun 
pf2vf_send_response(struct nitrox_device * ndev,struct nitrox_vfdev * vfdev)54*4882a593Smuzhiyun static void pf2vf_send_response(struct nitrox_device *ndev,
55*4882a593Smuzhiyun 				struct nitrox_vfdev *vfdev)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun 	union mbox_msg msg;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	msg.value = vfdev->msg.value;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	switch (vfdev->msg.opcode) {
62*4882a593Smuzhiyun 	case MSG_OP_VF_MODE:
63*4882a593Smuzhiyun 		msg.data = ndev->mode;
64*4882a593Smuzhiyun 		break;
65*4882a593Smuzhiyun 	case MSG_OP_VF_UP:
66*4882a593Smuzhiyun 		vfdev->nr_queues = vfdev->msg.data;
67*4882a593Smuzhiyun 		atomic_set(&vfdev->state, __NDEV_READY);
68*4882a593Smuzhiyun 		break;
69*4882a593Smuzhiyun 	case MSG_OP_CHIPID_VFID:
70*4882a593Smuzhiyun 		msg.id.chipid = ndev->idx;
71*4882a593Smuzhiyun 		msg.id.vfid = vfdev->vfno;
72*4882a593Smuzhiyun 		break;
73*4882a593Smuzhiyun 	case MSG_OP_VF_DOWN:
74*4882a593Smuzhiyun 		vfdev->nr_queues = 0;
75*4882a593Smuzhiyun 		atomic_set(&vfdev->state, __NDEV_NOT_READY);
76*4882a593Smuzhiyun 		break;
77*4882a593Smuzhiyun 	case MSG_OP_MCODE_INFO:
78*4882a593Smuzhiyun 		msg.data = 0;
79*4882a593Smuzhiyun 		msg.mcode_info.count = 2;
80*4882a593Smuzhiyun 		msg.mcode_info.info = MCODE_TYPE_SE_SSL | (MCODE_TYPE_AE << 5);
81*4882a593Smuzhiyun 		msg.mcode_info.next_se_grp = 1;
82*4882a593Smuzhiyun 		msg.mcode_info.next_ae_grp = 1;
83*4882a593Smuzhiyun 		break;
84*4882a593Smuzhiyun 	default:
85*4882a593Smuzhiyun 		msg.type = MBX_MSG_TYPE_NOP;
86*4882a593Smuzhiyun 		break;
87*4882a593Smuzhiyun 	}
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	if (msg.type == MBX_MSG_TYPE_NOP)
90*4882a593Smuzhiyun 		return;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	/* send ACK to VF */
93*4882a593Smuzhiyun 	msg.type = MBX_MSG_TYPE_ACK;
94*4882a593Smuzhiyun 	pf2vf_write_mbox(ndev, msg.value, vfdev->ring);
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	vfdev->msg.value = 0;
97*4882a593Smuzhiyun 	atomic64_inc(&vfdev->mbx_resp);
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun 
pf2vf_resp_handler(struct work_struct * work)100*4882a593Smuzhiyun static void pf2vf_resp_handler(struct work_struct *work)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun 	struct pf2vf_work *pf2vf_resp = container_of(work, struct pf2vf_work,
103*4882a593Smuzhiyun 						     pf2vf_resp);
104*4882a593Smuzhiyun 	struct nitrox_vfdev *vfdev = pf2vf_resp->vfdev;
105*4882a593Smuzhiyun 	struct nitrox_device *ndev = pf2vf_resp->ndev;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	switch (vfdev->msg.type) {
108*4882a593Smuzhiyun 	case MBX_MSG_TYPE_REQ:
109*4882a593Smuzhiyun 		/* process the request from VF */
110*4882a593Smuzhiyun 		pf2vf_send_response(ndev, vfdev);
111*4882a593Smuzhiyun 		break;
112*4882a593Smuzhiyun 	case MBX_MSG_TYPE_ACK:
113*4882a593Smuzhiyun 	case MBX_MSG_TYPE_NACK:
114*4882a593Smuzhiyun 		break;
115*4882a593Smuzhiyun 	};
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	kfree(pf2vf_resp);
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun 
nitrox_pf2vf_mbox_handler(struct nitrox_device * ndev)120*4882a593Smuzhiyun void nitrox_pf2vf_mbox_handler(struct nitrox_device *ndev)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun 	struct nitrox_vfdev *vfdev;
123*4882a593Smuzhiyun 	struct pf2vf_work *pfwork;
124*4882a593Smuzhiyun 	u64 value, reg_addr;
125*4882a593Smuzhiyun 	u32 i;
126*4882a593Smuzhiyun 	int vfno;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	/* loop for VF(0..63) */
129*4882a593Smuzhiyun 	reg_addr = NPS_PKT_MBOX_INT_LO;
130*4882a593Smuzhiyun 	value = nitrox_read_csr(ndev, reg_addr);
131*4882a593Smuzhiyun 	for_each_set_bit(i, (const unsigned long *)&value, BITS_PER_LONG) {
132*4882a593Smuzhiyun 		/* get the vfno from ring */
133*4882a593Smuzhiyun 		vfno = RING_TO_VFNO(i, ndev->iov.max_vf_queues);
134*4882a593Smuzhiyun 		vfdev = ndev->iov.vfdev + vfno;
135*4882a593Smuzhiyun 		vfdev->ring = i;
136*4882a593Smuzhiyun 		/* fill the vf mailbox data */
137*4882a593Smuzhiyun 		vfdev->msg.value = pf2vf_read_mbox(ndev, vfdev->ring);
138*4882a593Smuzhiyun 		pfwork = kzalloc(sizeof(*pfwork), GFP_ATOMIC);
139*4882a593Smuzhiyun 		if (!pfwork)
140*4882a593Smuzhiyun 			continue;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 		pfwork->vfdev = vfdev;
143*4882a593Smuzhiyun 		pfwork->ndev = ndev;
144*4882a593Smuzhiyun 		INIT_WORK(&pfwork->pf2vf_resp, pf2vf_resp_handler);
145*4882a593Smuzhiyun 		queue_work(ndev->iov.pf2vf_wq, &pfwork->pf2vf_resp);
146*4882a593Smuzhiyun 		/* clear the corresponding vf bit */
147*4882a593Smuzhiyun 		nitrox_write_csr(ndev, reg_addr, BIT_ULL(i));
148*4882a593Smuzhiyun 	}
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	/* loop for VF(64..127) */
151*4882a593Smuzhiyun 	reg_addr = NPS_PKT_MBOX_INT_HI;
152*4882a593Smuzhiyun 	value = nitrox_read_csr(ndev, reg_addr);
153*4882a593Smuzhiyun 	for_each_set_bit(i, (const unsigned long *)&value, BITS_PER_LONG) {
154*4882a593Smuzhiyun 		/* get the vfno from ring */
155*4882a593Smuzhiyun 		vfno = RING_TO_VFNO(i + 64, ndev->iov.max_vf_queues);
156*4882a593Smuzhiyun 		vfdev = ndev->iov.vfdev + vfno;
157*4882a593Smuzhiyun 		vfdev->ring = (i + 64);
158*4882a593Smuzhiyun 		/* fill the vf mailbox data */
159*4882a593Smuzhiyun 		vfdev->msg.value = pf2vf_read_mbox(ndev, vfdev->ring);
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 		pfwork = kzalloc(sizeof(*pfwork), GFP_ATOMIC);
162*4882a593Smuzhiyun 		if (!pfwork)
163*4882a593Smuzhiyun 			continue;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 		pfwork->vfdev = vfdev;
166*4882a593Smuzhiyun 		pfwork->ndev = ndev;
167*4882a593Smuzhiyun 		INIT_WORK(&pfwork->pf2vf_resp, pf2vf_resp_handler);
168*4882a593Smuzhiyun 		queue_work(ndev->iov.pf2vf_wq, &pfwork->pf2vf_resp);
169*4882a593Smuzhiyun 		/* clear the corresponding vf bit */
170*4882a593Smuzhiyun 		nitrox_write_csr(ndev, reg_addr, BIT_ULL(i));
171*4882a593Smuzhiyun 	}
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun 
nitrox_mbox_init(struct nitrox_device * ndev)174*4882a593Smuzhiyun int nitrox_mbox_init(struct nitrox_device *ndev)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	struct nitrox_vfdev *vfdev;
177*4882a593Smuzhiyun 	int i;
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	ndev->iov.vfdev = kcalloc(ndev->iov.num_vfs,
180*4882a593Smuzhiyun 				  sizeof(struct nitrox_vfdev), GFP_KERNEL);
181*4882a593Smuzhiyun 	if (!ndev->iov.vfdev)
182*4882a593Smuzhiyun 		return -ENOMEM;
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	for (i = 0; i < ndev->iov.num_vfs; i++) {
185*4882a593Smuzhiyun 		vfdev = ndev->iov.vfdev + i;
186*4882a593Smuzhiyun 		vfdev->vfno = i;
187*4882a593Smuzhiyun 	}
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	/* allocate pf2vf response workqueue */
190*4882a593Smuzhiyun 	ndev->iov.pf2vf_wq = alloc_workqueue("nitrox_pf2vf", 0, 0);
191*4882a593Smuzhiyun 	if (!ndev->iov.pf2vf_wq) {
192*4882a593Smuzhiyun 		kfree(ndev->iov.vfdev);
193*4882a593Smuzhiyun 		return -ENOMEM;
194*4882a593Smuzhiyun 	}
195*4882a593Smuzhiyun 	/* enable pf2vf mailbox interrupts */
196*4882a593Smuzhiyun 	enable_pf2vf_mbox_interrupts(ndev);
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	return 0;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun 
nitrox_mbox_cleanup(struct nitrox_device * ndev)201*4882a593Smuzhiyun void nitrox_mbox_cleanup(struct nitrox_device *ndev)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun 	/* disable pf2vf mailbox interrupts */
204*4882a593Smuzhiyun 	disable_pf2vf_mbox_interrupts(ndev);
205*4882a593Smuzhiyun 	/* destroy workqueue */
206*4882a593Smuzhiyun 	if (ndev->iov.pf2vf_wq)
207*4882a593Smuzhiyun 		destroy_workqueue(ndev->iov.pf2vf_wq);
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	kfree(ndev->iov.vfdev);
210*4882a593Smuzhiyun 	ndev->iov.pf2vf_wq = NULL;
211*4882a593Smuzhiyun 	ndev->iov.vfdev = NULL;
212*4882a593Smuzhiyun }
213