1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/workqueue.h>
4 #include "nitrox_csr.h"
5 #include "nitrox_hal.h"
6 #include "nitrox_dev.h"
7 #include "nitrox_mbx.h"
9 #define RING_TO_VFNO(_x, _y) ((_x) / (_y))
12 * mbx_msg_type - Mailbox message types
22 * mbx_msg_opcode - Mailbox message opcodes
29 MSG_OP_MCODE_INFO = 11,
33 struct nitrox_vfdev *vfdev;
34 struct nitrox_device *ndev;
35 struct work_struct pf2vf_resp;
38 static inline u64 pf2vf_read_mbox(struct nitrox_device *ndev, int ring)
42 reg_addr = NPS_PKT_MBOX_VF_PF_PFDATAX(ring);
43 return nitrox_read_csr(ndev, reg_addr);
46 static inline void pf2vf_write_mbox(struct nitrox_device *ndev, u64 value,
51 reg_addr = NPS_PKT_MBOX_PF_VF_PFDATAX(ring);
52 nitrox_write_csr(ndev, reg_addr, value);
55 static void pf2vf_send_response(struct nitrox_device *ndev,
56 struct nitrox_vfdev *vfdev)
60 msg.value = vfdev->msg.value;
62 switch (vfdev->msg.opcode) {
64 msg.data = ndev->mode;
67 vfdev->nr_queues = vfdev->msg.data;
68 atomic_set(&vfdev->state, __NDEV_READY);
70 case MSG_OP_CHIPID_VFID:
71 msg.id.chipid = ndev->idx;
72 msg.id.vfid = vfdev->vfno;
76 atomic_set(&vfdev->state, __NDEV_NOT_READY);
78 case MSG_OP_MCODE_INFO:
80 msg.mcode_info.count = 2;
81 msg.mcode_info.info = MCODE_TYPE_SE_SSL | (MCODE_TYPE_AE << 5);
82 msg.mcode_info.next_se_grp = 1;
83 msg.mcode_info.next_ae_grp = 1;
86 msg.type = MBX_MSG_TYPE_NOP;
90 if (msg.type == MBX_MSG_TYPE_NOP)
94 msg.type = MBX_MSG_TYPE_ACK;
95 pf2vf_write_mbox(ndev, msg.value, vfdev->ring);
98 atomic64_inc(&vfdev->mbx_resp);
101 static void pf2vf_resp_handler(struct work_struct *work)
103 struct pf2vf_work *pf2vf_resp = container_of(work, struct pf2vf_work,
105 struct nitrox_vfdev *vfdev = pf2vf_resp->vfdev;
106 struct nitrox_device *ndev = pf2vf_resp->ndev;
108 switch (vfdev->msg.type) {
109 case MBX_MSG_TYPE_REQ:
110 /* process the request from VF */
111 pf2vf_send_response(ndev, vfdev);
113 case MBX_MSG_TYPE_ACK:
114 case MBX_MSG_TYPE_NACK:
121 void nitrox_pf2vf_mbox_handler(struct nitrox_device *ndev)
123 struct nitrox_vfdev *vfdev;
124 struct pf2vf_work *pfwork;
129 /* loop for VF(0..63) */
130 reg_addr = NPS_PKT_MBOX_INT_LO;
131 value = nitrox_read_csr(ndev, reg_addr);
132 for_each_set_bit(i, (const unsigned long *)&value, BITS_PER_LONG) {
133 /* get the vfno from ring */
134 vfno = RING_TO_VFNO(i, ndev->iov.max_vf_queues);
135 vfdev = ndev->iov.vfdev + vfno;
137 /* fill the vf mailbox data */
138 vfdev->msg.value = pf2vf_read_mbox(ndev, vfdev->ring);
139 pfwork = kzalloc(sizeof(*pfwork), GFP_ATOMIC);
143 pfwork->vfdev = vfdev;
145 INIT_WORK(&pfwork->pf2vf_resp, pf2vf_resp_handler);
146 queue_work(ndev->iov.pf2vf_wq, &pfwork->pf2vf_resp);
147 /* clear the corresponding vf bit */
148 nitrox_write_csr(ndev, reg_addr, BIT_ULL(i));
151 /* loop for VF(64..127) */
152 reg_addr = NPS_PKT_MBOX_INT_HI;
153 value = nitrox_read_csr(ndev, reg_addr);
154 for_each_set_bit(i, (const unsigned long *)&value, BITS_PER_LONG) {
155 /* get the vfno from ring */
156 vfno = RING_TO_VFNO(i + 64, ndev->iov.max_vf_queues);
157 vfdev = ndev->iov.vfdev + vfno;
158 vfdev->ring = (i + 64);
159 /* fill the vf mailbox data */
160 vfdev->msg.value = pf2vf_read_mbox(ndev, vfdev->ring);
162 pfwork = kzalloc(sizeof(*pfwork), GFP_ATOMIC);
166 pfwork->vfdev = vfdev;
168 INIT_WORK(&pfwork->pf2vf_resp, pf2vf_resp_handler);
169 queue_work(ndev->iov.pf2vf_wq, &pfwork->pf2vf_resp);
170 /* clear the corresponding vf bit */
171 nitrox_write_csr(ndev, reg_addr, BIT_ULL(i));
175 int nitrox_mbox_init(struct nitrox_device *ndev)
177 struct nitrox_vfdev *vfdev;
180 ndev->iov.vfdev = kcalloc(ndev->iov.num_vfs,
181 sizeof(struct nitrox_vfdev), GFP_KERNEL);
182 if (!ndev->iov.vfdev)
185 for (i = 0; i < ndev->iov.num_vfs; i++) {
186 vfdev = ndev->iov.vfdev + i;
190 /* allocate pf2vf response workqueue */
191 ndev->iov.pf2vf_wq = alloc_workqueue("nitrox_pf2vf", 0, 0);
192 if (!ndev->iov.pf2vf_wq) {
193 kfree(ndev->iov.vfdev);
196 /* enable pf2vf mailbox interrupts */
197 enable_pf2vf_mbox_interrupts(ndev);
202 void nitrox_mbox_cleanup(struct nitrox_device *ndev)
204 /* disable pf2vf mailbox interrupts */
205 disable_pf2vf_mbox_interrupts(ndev);
206 /* destroy workqueue */
207 if (ndev->iov.pf2vf_wq)
208 destroy_workqueue(ndev->iov.pf2vf_wq);
210 kfree(ndev->iov.vfdev);
211 ndev->iov.pf2vf_wq = NULL;
212 ndev->iov.vfdev = NULL;