Path: blob/master/drivers/crypto/cavium/nitrox/nitrox_mbx.c
26285 views
// SPDX-License-Identifier: GPL-2.01#include <linux/bitmap.h>2#include <linux/workqueue.h>34#include "nitrox_csr.h"5#include "nitrox_hal.h"6#include "nitrox_dev.h"7#include "nitrox_mbx.h"89#define RING_TO_VFNO(_x, _y) ((_x) / (_y))1011/*12* mbx_msg_type - Mailbox message types13*/14enum mbx_msg_type {15MBX_MSG_TYPE_NOP,16MBX_MSG_TYPE_REQ,17MBX_MSG_TYPE_ACK,18MBX_MSG_TYPE_NACK,19};2021/*22* mbx_msg_opcode - Mailbox message opcodes23*/24enum mbx_msg_opcode {25MSG_OP_VF_MODE = 1,26MSG_OP_VF_UP,27MSG_OP_VF_DOWN,28MSG_OP_CHIPID_VFID,29MSG_OP_MCODE_INFO = 11,30};3132struct pf2vf_work {33struct nitrox_vfdev *vfdev;34struct nitrox_device *ndev;35struct work_struct pf2vf_resp;36};3738static inline u64 pf2vf_read_mbox(struct nitrox_device *ndev, int ring)39{40u64 reg_addr;4142reg_addr = NPS_PKT_MBOX_VF_PF_PFDATAX(ring);43return nitrox_read_csr(ndev, reg_addr);44}4546static inline void pf2vf_write_mbox(struct nitrox_device *ndev, u64 value,47int ring)48{49u64 reg_addr;5051reg_addr = NPS_PKT_MBOX_PF_VF_PFDATAX(ring);52nitrox_write_csr(ndev, reg_addr, value);53}5455static void pf2vf_send_response(struct nitrox_device *ndev,56struct nitrox_vfdev *vfdev)57{58union mbox_msg msg;5960msg.value = vfdev->msg.value;6162switch (vfdev->msg.opcode) {63case MSG_OP_VF_MODE:64msg.data = ndev->mode;65break;66case MSG_OP_VF_UP:67vfdev->nr_queues = vfdev->msg.data;68atomic_set(&vfdev->state, __NDEV_READY);69break;70case MSG_OP_CHIPID_VFID:71msg.id.chipid = ndev->idx;72msg.id.vfid = vfdev->vfno;73break;74case MSG_OP_VF_DOWN:75vfdev->nr_queues = 0;76atomic_set(&vfdev->state, __NDEV_NOT_READY);77break;78case MSG_OP_MCODE_INFO:79msg.data = 0;80msg.mcode_info.count = 2;81msg.mcode_info.info = MCODE_TYPE_SE_SSL | (MCODE_TYPE_AE << 5);82msg.mcode_info.next_se_grp = 1;83msg.mcode_info.next_ae_grp = 1;84break;85default:86msg.type = MBX_MSG_TYPE_NOP;87break;88}8990if (msg.type == MBX_MSG_TYPE_NOP)91return;9293/* send ACK to VF */94msg.type = MBX_MSG_TYPE_ACK;95pf2vf_write_mbox(ndev, msg.value, vfdev->ring);9697vfdev->msg.value = 0;98atomic64_inc(&vfdev->mbx_resp);99}100101static void pf2vf_resp_handler(struct work_struct *work)102{103struct pf2vf_work *pf2vf_resp = container_of(work, struct pf2vf_work,104pf2vf_resp);105struct nitrox_vfdev *vfdev = pf2vf_resp->vfdev;106struct nitrox_device *ndev = pf2vf_resp->ndev;107108switch (vfdev->msg.type) {109case MBX_MSG_TYPE_REQ:110/* process the request from VF */111pf2vf_send_response(ndev, vfdev);112break;113case MBX_MSG_TYPE_ACK:114case MBX_MSG_TYPE_NACK:115break;116}117118kfree(pf2vf_resp);119}120121void nitrox_pf2vf_mbox_handler(struct nitrox_device *ndev)122{123DECLARE_BITMAP(csr, BITS_PER_TYPE(u64));124struct nitrox_vfdev *vfdev;125struct pf2vf_work *pfwork;126u64 value, reg_addr;127u32 i;128int vfno;129130/* loop for VF(0..63) */131reg_addr = NPS_PKT_MBOX_INT_LO;132value = nitrox_read_csr(ndev, reg_addr);133bitmap_from_u64(csr, value);134for_each_set_bit(i, csr, BITS_PER_TYPE(csr)) {135/* get the vfno from ring */136vfno = RING_TO_VFNO(i, ndev->iov.max_vf_queues);137vfdev = ndev->iov.vfdev + vfno;138vfdev->ring = i;139/* fill the vf mailbox data */140vfdev->msg.value = pf2vf_read_mbox(ndev, vfdev->ring);141pfwork = kzalloc(sizeof(*pfwork), GFP_ATOMIC);142if (!pfwork)143continue;144145pfwork->vfdev = vfdev;146pfwork->ndev = ndev;147INIT_WORK(&pfwork->pf2vf_resp, pf2vf_resp_handler);148queue_work(ndev->iov.pf2vf_wq, &pfwork->pf2vf_resp);149/* clear the corresponding vf bit */150nitrox_write_csr(ndev, reg_addr, BIT_ULL(i));151}152153/* loop for VF(64..127) */154reg_addr = NPS_PKT_MBOX_INT_HI;155value = nitrox_read_csr(ndev, reg_addr);156bitmap_from_u64(csr, value);157for_each_set_bit(i, csr, BITS_PER_TYPE(csr)) {158/* get the vfno from ring */159vfno = RING_TO_VFNO(i + 64, ndev->iov.max_vf_queues);160vfdev = ndev->iov.vfdev + vfno;161vfdev->ring = (i + 64);162/* fill the vf mailbox data */163vfdev->msg.value = pf2vf_read_mbox(ndev, vfdev->ring);164165pfwork = kzalloc(sizeof(*pfwork), GFP_ATOMIC);166if (!pfwork)167continue;168169pfwork->vfdev = vfdev;170pfwork->ndev = ndev;171INIT_WORK(&pfwork->pf2vf_resp, pf2vf_resp_handler);172queue_work(ndev->iov.pf2vf_wq, &pfwork->pf2vf_resp);173/* clear the corresponding vf bit */174nitrox_write_csr(ndev, reg_addr, BIT_ULL(i));175}176}177178int nitrox_mbox_init(struct nitrox_device *ndev)179{180struct nitrox_vfdev *vfdev;181int i;182183ndev->iov.vfdev = kcalloc(ndev->iov.num_vfs,184sizeof(struct nitrox_vfdev), GFP_KERNEL);185if (!ndev->iov.vfdev)186return -ENOMEM;187188for (i = 0; i < ndev->iov.num_vfs; i++) {189vfdev = ndev->iov.vfdev + i;190vfdev->vfno = i;191}192193/* allocate pf2vf response workqueue */194ndev->iov.pf2vf_wq = alloc_workqueue("nitrox_pf2vf", 0, 0);195if (!ndev->iov.pf2vf_wq) {196kfree(ndev->iov.vfdev);197ndev->iov.vfdev = NULL;198return -ENOMEM;199}200/* enable pf2vf mailbox interrupts */201enable_pf2vf_mbox_interrupts(ndev);202203return 0;204}205206void nitrox_mbox_cleanup(struct nitrox_device *ndev)207{208/* disable pf2vf mailbox interrupts */209disable_pf2vf_mbox_interrupts(ndev);210/* destroy workqueue */211if (ndev->iov.pf2vf_wq)212destroy_workqueue(ndev->iov.pf2vf_wq);213214kfree(ndev->iov.vfdev);215ndev->iov.pf2vf_wq = NULL;216ndev->iov.vfdev = NULL;217}218219220